将 FrameworkElementFactory 绑定到数据对象属性

发布于 2024-07-14 03:19:52 字数 2283 浏览 1 评论 0原文

我有一个 ListBox ,其 DataTemplate 是使用 3 个 FrameworkElementFactory 对象(带有 2 个附加子项的 StackPanel )在代码中创建的(CheckBox > 和文本框))。 集合中绑定到 ListBoxItemsSource 的项目对象基本上与您通常在任何类型的 ListControl 中看到的 Item 对象类型相同。 我想要做的是将 DataTemplate 中的 CheckBoxIsChecked 属性绑定到 Item 对象上的布尔属性。 列表框支持3种模式,单选、多选和多选。 我尝试实现的模式是多重检查,以便将 CheckBoxIsChecked 属性绑定到项目对象的 Selected 属性。 这将创建一种行为,其中仅当 ListBoxItem 上的 CheckBoxIsChecked 属性为 true 时(而不是 WPF < Strong>ListBoxItem 的 IsSelected 属性为 true。 应该发生的情况是,数据对象上的布尔属性应该绑定到 IsChecked 属性,并且当 IsChecked 属性更改时,项目对象上的 Selected 属性将更新,从而更新 <幕后的strong>SelectedItems集合。

这是我刚刚描述的一些简化代码。

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;  
}

我没有包含一些代码,它们是 CheckBox 上的事件处理程序。 如果 Wpf ListBox 上存在多项选择,则该范围内的所有复选框都将切换为被单击的复选框的值。 我可以手动将项目上的 Selected 属性设置为发件人的 IsChecked 属性,一切正常,但我认为数据绑定应该可以正常工作,而不必手动执行此操作。 在这种情况下,数据绑定是异步的还是我需要显式地执行某些操作?

I have a ListBox whose DataTemplate is created in code using 3 FrameworkElementFactory objects(A StackPanel with 2 appended children(CheckBox and TextBox)). The item object in the collection that is bound to the ItemsSource of the ListBox is basically the same type of Item object that you would typically see with any type of ListControl. What I'm trying to do is bind the CheckBox's IsChecked property in the DataTemplate to a boolean property on the Item object. The ListBox supports 3 modes, single select, multiselect, and multicheck. The mode I'm trying to implement is multicheck so that the IsChecked property of the CheckBox is bound to the Selected property of the item object. This creates a behavior where the item is only considered selected when the CheckBox's IsChecked property on the ListBoxItem is true, not when the WPF ListBoxItem's IsSelected property is true. What should happen is that the boolean property on the data object should be bound to the IsChecked property, and when the IsChecked property is changed the Selected property on the item object will update, and will thus update a SelectedItems collection behind the scenes.

Here is some simplified code that I have 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 some code that I'm not including that is the event handler on the CheckBox. If there is a multiple selection on the Wpf ListBox, then all of the CheckBoxes in the range would be toggled to the value of the CheckBox that was clicked. I can manually set the Selected property on the Item to the IsChecked property of the sender and everything works fine, I would however think that databinding should just work and I wouldn't have to do this manually. Would the databinding in this case be asynchronous or do I need to do something explicitly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文