I've written a MultiValueConverter which checks if a given list contains a given value and returns true if it does. I use it for binding to custom checkbox list. Now I'd like to write ConvertBack method so that if checkbox was checked, original value would be sent to the model. Is there a way to access values in ConvertBack method?
XAML:
<ListBox.ItemTemplate>
<HierarchicalDataTemplate>
<CheckBox Content="{Binding Path=Description}">
<CheckBox.IsChecked>
<MultiBinding Converter="{x:Static Classes:ListContainsMultiConverter.Instance}">
<Binding Path="Id" />
<Binding Path="DataContext.ContactTypes" RelativeSource="{RelativeSource AncestorType={x:Type Window}}" />
</MultiBinding>
</CheckBox.IsChecked>
</CheckBox>
</HierarchicalDataTemplate>
</ListBox.ItemTemplate>
I get correct results when I'm binding but is there a way to get the bound id when converting back? What I would like to achieve is that if checkbox is unchecked, the value would be removed from the list and if it is checked, the value would be added to the list.
I know this is an old issue, but this solution might help someone else.
Instead of using the the ConvertBack
method of the IMultiValueConverter
, you can set the IsChecked
binding to be OneWay
and use the CheckBox
Command
property to perform the check logic.
<ListBox.ItemTemplate>
<HierarchicalDataTemplate>
<CheckBox Content="{Binding Path=.}" Command="{Binding Path=CheckBoxCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" CommandParameter="{Binding Path=.}">
<CheckBox.IsChecked>
<MultiBinding Converter="{x:Static Classes:ListContainsMultiConverter.Instance}" Mode="OneWay">
<Binding Path="." />
<Binding Path="SelectedItems" RelativeSource="{RelativeSource AncestorType={x:Type Window}}" />
</MultiBinding>
</CheckBox.IsChecked>
</CheckBox>
</HierarchicalDataTemplate>
</ListBox.ItemTemplate>
Then, add the CheckBoxCommand which performs something similar to this:
// the items bound to your checkbox
public Collection<string> Items { get; set; }
// the list of selected items
public Collection<string> SelectedItems { get; set; }
private void ExecuteCheckBoxCommand(string obj)
{
SelectedItems.Add(obj);
}
I know it's a slightly round-about way of doing this but the IMultiValueConverter.ConvertBack
method is really quite useless - I can't think of too many uses for it without having access to the current binding values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With