【发布时间】:2014-02-26 09:56:31
【问题描述】:
例如,我有一个带有自定义 DataTemplate 的 ItemsControl:
<ItemsControl Name="CategoriesList">
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
例如,我的集合包含 100 个元素。我想知道哪些 CheckBoxes 被选中,哪些不是,或者我只想更改 Content 属性。在这两种情况下,我都需要从代码中获取 CheckBox 元素。所以很容易从代码中获取Items:
var cList = CategoriesList.Items;
foreach (var item in cList)
{
//Do Something
}
但我需要从这些项目中获取 CheckBoxes。有可能吗?
谢谢!
【问题讨论】:
-
您可以使用 Aman Khandelwal 的解决方案,但我认为它过于复杂。通过属性类型 bool MyIsChecked 和绑定复选框 IsChecked 属性更容易扩展集合中的对象。
<CheckBox Content="{Binding MyContent}" IsChecked="{Binding MyIsChecked}"/>
标签: c# windows-phone-8