Project Type: .NET 4.0 WPF Desktop Application
Greetings.
I'm currently working on a solution to utilize IMultiValueConverters in a WPF application to bind the SelectedItem
properties of two ComboBoxes to the IsEnabled
property of a button. The ComboBoxes are placed in separate UserControls which are nested within the MainWindow along with the Button itself.
MainWindow.xaml
<Window>
<Window.Resources>
<local:MultiNullToBoolConverter x:Key="MultiNullToBoolConverter" />
</Window.Resources>
<Grid>
<local:ucDatabaseSelection x:Name="ucSourceDatabase" />
<local:ucDatabaseSelection x:Name="ucTargetDatabase" />
<Button x:Name="btnContinue">
<Button.IsEnabled>
<MultiBinding Converter="{StaticResource MultiNullToBoolConverter}">
<Binding ElementName="ucSourceDatabase" Path="cbxServerDatabaseCollection.SelectedItem" />
<Binding ElementName="ucTargetDatabase" Path="cbxServerDatabaseCollection.SelectedItem" />
</MultiBinding>
</Button.IsEnabled>
</Button>
</Grid>
</Window>
ucDatabaseSelection.xaml
<UserControl>
<ComboBox x:Name="cbxServerDatabaseCollection">
<ComboBoxItem Content="Server A" />
<ComboBoxItem Content="Server B" />
</ComboBox>
</UserControl>
MultiNullToBoolConverter.cs
/// <summary>
/// Converts two objects (values[0] and values[1]) to boolean
/// </summary>
/// <returns>TRUE if both objects are not null; FALSE if at least one object is null</returns>
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values[0] != null && values[1] != null) return true;
else return false;
}
The IsEnabled
property of the Button should only be true, when the SelectedItem
properties of both ComboBoxes are not null.
The Issue I have now is that I can't get the Binding to work from the MainWindow Button through the UserControls and onto the ComboBoxes. Am I missing UpdateTriggers here or is it simply not possible to bind it directly without using DependencyProperties in the UserControl class?
WPF data binding works with public properties only. Hence, the UserControl needs to have a public property that returns the
cbxServerDatabaseCollection
field value, e.g: