I have the following item data class, as well as a converter.
class ListBoxViewItem
{
public String Name { get; set; }
public Boolean IsChecked { get; set; }
}
[ValueConversion(typeof(List<String>),typeof(List<ListBoxViewItem>))]
class ListToItemConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null) return null;
IEnumerable<String> l = value as IEnumerable<String>;
return (from n in l select new ListBoxViewItem() { IsChecked = true, Name = n });
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
class ListBoxData
{
public List<String> AllData
{
get
{
return new List<string>()
{
"FOO",
"BAR"
};
}
set
{
}
}
}
I bind the instance of the ListBoxData
to a listbox control's ItemsSource
. As below:
<ListBox>
<ListBox.ItemsSource>
<Binding>
<Binding.Path>AllData</Binding.Path>
<Binding.Converter>
<local:ListToItemConverter />
</Binding.Converter>
<Binding.Mode>TwoWay</Binding.Mode>
</Binding>
</ListBox.ItemsSource>
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsChecked,Mode=TwoWay}"
Content="{Binding Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
So My question is, the Convert
function is called when the listbox is showing, but as every items in this list box is a checkbox, although I use TwoWay
binding to bind the instance and listbox, but ConvertBack
is not called when I check/uncheck the checkbox in this listbox.
I am not sure if ConvertBack
is designed to work as I expected. But if I want the ConvertBack
can be triggered when check status is changed, what should I do ?