【发布时间】:2012-01-09 17:21:29
【问题描述】:
我有一个复选框列表框,我想遍历并选中它们,然后取消选中它们。我还需要找到稍后检查的那些。这是我的代码:
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Style="{StaticResource TextLabel}" Text="Building Houses Organization:"></TextBlock>
<TextBlock Text="All" Margin="136,0,0,0" Foreground="#FF001BFF" FontSize="11" VerticalAlignment="Center" Tag="{Binding ElementName=BuildingsOrganizationList}" MouseLeftButtonDown="CheckAll"/>
<TextBlock Text=" | " VerticalAlignment="Center"/>
<TextBlock Text="None" Foreground="#FF001BFF" FontSize="11" VerticalAlignment="Center" Tag="{Binding ElementName=BuildingsOrganizationList}" MouseLeftButtonDown="CheckNone"/>
</StackPanel>
<ListBox x:Name="BuildingsOrganizationList" HorizontalAlignment="Left" VerticalAlignment="Top" Width="{Binding ActualWidth, ElementName=BuildingOrganizationGrid, Mode=OneWay}" Height="141">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
你可以看到我的文本块有一个标签绑定到我的列表框,其中包含我的复选框。现在在后面的代码中我有以下内容:
private void CheckAll(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
TextBlock textblock = sender as TextBlock;
ListBox list = textblock.Tag as ListBox;
foreach (ListBoxItem item in list.Items)
{
//.....
}
}
问题是项目是字符串。它们不是 ListBoxItem 或 CheckBox 对象。这是为什么呢?
编辑
我现在已经添加了这个类
class ListItem
{
private string _name;
private bool? _isChecked;
public ListItem()
{
_name = "";
_isChecked = null;
}
public ListItem(string name, bool? isChecked)
{
Name = name;
IsChecked = isChecked;
}
public string Name
{
get { return _name; }
set { _name = value; }
}
public bool? IsChecked
{
get { return _isChecked; }
set { _isChecked = value; }
}
}
并有以下几行来填充我的列表
BuildingsOrganizationList.Items.Add(new ListItem(org, true));
还有 XAML:
<ListBox x:Name="BuildingsOrganizationList" HorizontalAlignment="Left" VerticalAlignment="Top" Width="{Binding ActualWidth, ElementName=BuildingOrganizationGrid, Mode=OneWay}" Height="141">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Name}" IsChecked="{Binding IsChecked}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
但是,我的复选框没有被选中,也没有文字。
【问题讨论】:
标签: c# .net wpf silverlight xaml