Silverlight+ItemsControl+Get控件属性值
我有一个ItemsControl,其中包含一个已定义的DataTemplate。我的ItemsControl定义如下所示:Silverlight+ItemsControl+Get控件属性值,silverlight,Silverlight,我有一个ItemsControl,其中包含一个已定义的DataTemplate。我的ItemsControl定义如下所示: <ItemsControl x:Name="myItemsControl" ItemsSource="{Binding}"> <ItemsControl.ItemTemplate> <DataTemplate> <Grid> <CheckBox x:Name="myCheckBox
<ItemsControl x:Name="myItemsControl" ItemsSource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<CheckBox x:Name="myCheckBox" Content="{Binding Name}" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
这是我的DataTemplate的简化版本。不管怎样,当用户单击页面上的按钮时,我希望在myItemsControl中的项目之间循环,并确定是否选中了与该项目关联的复选框元素
如何确定是否选中ItemsControl中特定项的复选框
谢谢大家! 向数据类添加属性并对其进行数据绑定,然后迭代集合本身
public class myDataClass
{
public string Name { get; set;}
public bool IsSomething { get; set; }
}
<CheckBox x:Name="myCheckBox" Content="{Binding Name}" IsChecked="{Binding IsChecked, Mode=TwoWay}" />
向数据类添加属性并对其进行数据绑定,然后迭代集合本身
public class myDataClass
{
public string Name { get; set;}
public bool IsSomething { get; set; }
}
<CheckBox x:Name="myCheckBox" Content="{Binding Name}" IsChecked="{Binding IsChecked, Mode=TwoWay}" />
您可以尝试类似于传统迭代的方法:
public bool? TestMyCheckbox(string bindingName)
{
foreach (var item in myItemsControl.Items)
{
if (item.GetType() == typeof(CheckBox))
{
var checkbox = (CheckBox)item;
if (checkbox.Content.Equals(bindingName))
{
return (checkbox.IsChecked);
}
}
}
return null;
}
此外,这可能更适合您的需要。您可以查找选中的复选框绑定列表:
public IEnumerable<object> TestMyCheckboxes(ItemsControl control)
{
return from Control x in control.Items
where x.GetType().Equals(typeof(CheckBox)) && ((CheckBox)x).IsChecked == true
select ((CheckBox)x).Content;
}
您可以尝试类似于传统迭代的方法:
public bool? TestMyCheckbox(string bindingName)
{
foreach (var item in myItemsControl.Items)
{
if (item.GetType() == typeof(CheckBox))
{
var checkbox = (CheckBox)item;
if (checkbox.Content.Equals(bindingName))
{
return (checkbox.IsChecked);
}
}
}
return null;
}
此外,这可能更适合您的需要。您可以查找选中的复选框绑定列表:
public IEnumerable<object> TestMyCheckboxes(ItemsControl control)
{
return from Control x in control.Items
where x.GetType().Equals(typeof(CheckBox)) && ((CheckBox)x).IsChecked == true
select ((CheckBox)x).Content;
}
是否有一种方法可以实际获取复选框而不是数据源本身?我需要更改一些UI颜色等等。这就是为什么我试图访问复选框。总有办法的。但是,在ItemsControl中手动处理可视项是最糟糕的做法。如果您需要您的项目响应数据更改,请考虑将其封装在UserControls中,并使用VisualStaseMeNeGER状态来响应这些数据更改。难道没有办法实际获得复选框而不是数据源本身吗?我需要更改一些UI颜色等等。这就是为什么我试图访问复选框。总有办法的。但是,在ItemsControl中手动处理可视项是最糟糕的做法。如果您需要您的项目响应数据更改,请考虑将其封装在UserControls中,并使用VisualStaseMeNeGER状态来响应这些数据更改。