Checkbox trigger conditions binding
-
How can I bind a checkbox IsChecked value to a condition from a combobox?
<ComboBox x:Name="airport"
ItemsSource="{Binding Path=AirportList}"
SelectedValuePath="AirportID"
SelectedValue="{Binding Path=CurrentItem.AirportID}"/>
<CheckBox Content="International" IsChecked="{Binding Path=CurrentItem.International}">
<CheckBox.Template>
<ControlTemplate TargetType="CheckBox">
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Binding="{Binding ElementName=airport, Path=SelectedItem.CountryID}" Value="{Binding Path=CurrentItem.ThisCountryID}" />
</MultiTrigger.Conditions>
<Setter Property="CheckBox.IsChecked" Value="True" />
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</CheckBox.Template>
</CheckBox>I'm using MVVM, and the VM has
public AirportDestination CurrentItem = new AirportDestination() //This record - ID, ThisCountryID, AirportID, International
public ObservableCollection<LikelyDestinations> AirportList = new ObservableCollection<LikelyDestinations>();//Airport Listing - AirportID, CountryID, NameThe problem is the Condition value binding. You can't! I want to change the IsChecked based on the condition that the selected combobox SelectedItem.CountryID equals ThisCountryID and also hold the value in CurrentItem.International Anybody have a clue how to do this in XAML only!
-
How can I bind a checkbox IsChecked value to a condition from a combobox?
<ComboBox x:Name="airport"
ItemsSource="{Binding Path=AirportList}"
SelectedValuePath="AirportID"
SelectedValue="{Binding Path=CurrentItem.AirportID}"/>
<CheckBox Content="International" IsChecked="{Binding Path=CurrentItem.International}">
<CheckBox.Template>
<ControlTemplate TargetType="CheckBox">
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Binding="{Binding ElementName=airport, Path=SelectedItem.CountryID}" Value="{Binding Path=CurrentItem.ThisCountryID}" />
</MultiTrigger.Conditions>
<Setter Property="CheckBox.IsChecked" Value="True" />
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</CheckBox.Template>
</CheckBox>I'm using MVVM, and the VM has
public AirportDestination CurrentItem = new AirportDestination() //This record - ID, ThisCountryID, AirportID, International
public ObservableCollection<LikelyDestinations> AirportList = new ObservableCollection<LikelyDestinations>();//Airport Listing - AirportID, CountryID, NameThe problem is the Condition value binding. You can't! I want to change the IsChecked based on the condition that the selected combobox SelectedItem.CountryID equals ThisCountryID and also hold the value in CurrentItem.International Anybody have a clue how to do this in XAML only!
In XAML only, I don't think you can (Would love to be proven wrong), but you could keep it generic with a MultiBinding...
<MultiBinding Converter="{StaticResource EqualityConverter}">
<Binding ElementName="airport" Path="SelectedItem.CountryID"/>
<Binding Path="CurrentItem.CountryID"/>
</MultiBinding>You have to make the EqualityConverter (Or whatever you want to name it) in the code somewhere... Just make a class that inherits IMultiValueConverter and returns true only if the two values are equal.
Proud to have finally moved to the A-Ark. Which one are you in? Author of Guardians of Xen (Sci-Fi/Fantasy novel)
-
In XAML only, I don't think you can (Would love to be proven wrong), but you could keep it generic with a MultiBinding...
<MultiBinding Converter="{StaticResource EqualityConverter}">
<Binding ElementName="airport" Path="SelectedItem.CountryID"/>
<Binding Path="CurrentItem.CountryID"/>
</MultiBinding>You have to make the EqualityConverter (Or whatever you want to name it) in the code somewhere... Just make a class that inherits IMultiValueConverter and returns true only if the two values are equal.
Proud to have finally moved to the A-Ark. Which one are you in? Author of Guardians of Xen (Sci-Fi/Fantasy novel)
Thanks for the update. It's kind of working :confused: When it updates, it changes the Content to either True or False but does not actually check the box??? I don't what the Content to change just the IsChecked value... any clues?
-
Thanks for the update. It's kind of working :confused: When it updates, it changes the Content to either True or False but does not actually check the box??? I don't what the Content to change just the IsChecked value... any clues?
Well it looks like you've already bound the IsChecked property. Try binding that to this MultiBinding instead of making a whole template for it.
Proud to have finally moved to the A-Ark. Which one are you in? Author of Guardians of Xen (Sci-Fi/Fantasy novel)
-
Well it looks like you've already bound the IsChecked property. Try binding that to this MultiBinding instead of making a whole template for it.
Proud to have finally moved to the A-Ark. Which one are you in? Author of Guardians of Xen (Sci-Fi/Fantasy novel)
This updates the Checkbox nicely. The values SelectedItem.CountryID and CurrentItem.ThisCountryID are compared to each other via the converter
<CheckBox FontSize="12" HorizontalAlignment="Left" VerticalAlignment="Center">
<CheckBox.IsChecked>
<MultiBinding Converter="{StaticResource BooleanConverter}">
<Binding ElementName="airport" Path="SelectedItem.CountryID" />
<Binding Path="CurrentItem.ThisCountryID" />
<Binding Path="CurrentItem.International" Mode="OneWayToSource" />
</MultiBinding>
</CheckBox.IsChecked>
</CheckBox>However, I can't get the final binding to bind, CurrentItem.International is the field stored in db
-
This updates the Checkbox nicely. The values SelectedItem.CountryID and CurrentItem.ThisCountryID are compared to each other via the converter
<CheckBox FontSize="12" HorizontalAlignment="Left" VerticalAlignment="Center">
<CheckBox.IsChecked>
<MultiBinding Converter="{StaticResource BooleanConverter}">
<Binding ElementName="airport" Path="SelectedItem.CountryID" />
<Binding Path="CurrentItem.ThisCountryID" />
<Binding Path="CurrentItem.International" Mode="OneWayToSource" />
</MultiBinding>
</CheckBox.IsChecked>
</CheckBox>However, I can't get the final binding to bind, CurrentItem.International is the field stored in db
The multibinding can't go in two directions... A multibinding combines two or more bindings to set one value... It doesn't do OneWayToSource.
Proud to have finally moved to the A-Ark. Which one are you in? Author of Guardians of Xen (Sci-Fi/Fantasy novel)