I have a view model with a list of items with the Checked
property.
In my WPF XAML, I create a ListBox
whose items have a checkbox each in their DataTemplate
.
Also, the view model has a property AllowMultiSelect
. If true, the interface must allow the user to check many boxes. But if false, the interface should show RadioButton
s instead and the user should select only one.
I tried a fancy XAML containing both a CheckBox
and a RadioButton
for the items, setting their Visibility
based on the value of AllowMultiSelect
with help of some derived properties.
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type local:VMType}">
<StackPanel Orientation="Horizontal" Margin="0 5 0 0">
<CheckBox IsChecked="{Binding IsChecked}"
Visibility="{Binding Path=DataContext.ShowTableCheck,
ElementName=ListBoxODTables,
Converter={StaticResource BoolToVisible}}"/>
<RadioButton IsChecked="{Binding IsChecked}" GroupName="TableRadios"
Visibility="{Binding Path=DataContext.ShowTableRadio,
ElementName=ListBoxODTables,
Converter={StaticResource BoolToVisible}}"/>
<TextBlock Text="{Binding Text}" Margin="5 0 0 0"/>
</StackPanel>
But the RadioButton
(even hidden) is affecting the selection. As I click a checkbox, the Checked
property of the item changes, this change reflects in the hidden RadioButton
, then the radio automatically deselects other items.
Question: is there a way to “bind only if” a property is true? if AllowMultiSelect
is true, I don’t want to bind the radio buttons.