I have a ListBox whose DataTemplate is created in code using 3 FrameworkElementFactory objects (A StackPanel with two child children ( CheckBox and TextBox )). The item in the collection bound to the ItemsSource ListBox is basically the same type of Item that you usually see with any type of ListControl . I am trying to associate the CheckBox IsChecked property in a DataTemplate with the boolean property of an Item object. ListBox supports 3 modes, one selection, multi-selection and multi-screen mode. The mode I'm trying to implement is multicheck, so the IsChecked CheckBox property is bound to the Selected property of the item object. This creates behavior when the element is considered only selected if the CheckBox IsChecked property in the ListBoxItem is true, and not when the WPF ListBoxItem IsSelected property is true. What should happen is that the logical property of the data object must be bound to the IsChecked property, and when the IsChecked property changes, the Selected object property of the object will be updated and thus update the value of the <strong collection> SelectedItems backstage.
Here is some simplified code that I just described.
ListBox innerListBox = new ListBox(); //The ItemsSource of the ListBox being set to the collection of items this.innerListBox.ItemsSource = this.Manager.ItemManagers; this.innerListBox.ItemTemplate = this.GetMultipleCheckTemplate(); public System.Windows.DataTemplate GetMultipleCheckTemplate() { DataTemplate dt = new DataTemplate; FrameworkElementFactory factorySP = new FrameworkElementFactory(typeof(StackPanel)); FrameworkElementFactory factoryCB = new FrameworkElementFactory(typeof(CheckBox)); factoryCB.SetBinding(CheckBox.IsCheckedProperty, new Binding("Selected"); RoutedEventHandler clickHandler = new RoutedEventHandler(ItemCheckBox_Click); factoryCheckBox.AddHandler(CheckBox.ClickEvent, clickHandler, true); factorySP.AppendChild(factoryCB); FrameworkElementFactory factoryTB = new FrameworkElementFactory(typeof(TextBlock)); factoryTB .SetBinding(TextBlock.TextProperty, new Binding("Description"); factorySP.AppendChild(factoryTB); template.VisualTree = factorySP; return template; }
There is code in CheckBox code that I donβt include, it is an event handler. If there is multiple selection in the Wpf ListBox , then all the checkboxes in the range will be switched to the CheckBox value that was clicked. I can manually set the Selected property on Item to the sender's IsChecked property, and everything works fine, however, I would think that data binding should work, and I would not have to do it manually. Will the data binding in this case be asynchronous or do I need to do something explicitly?
source share