C# 如何在WPF复选框列表框中获取所选项目
我正在使用列表框项目中的复选框,如何从列表中获取所选复选框C# 如何在WPF复选框列表框中获取所选项目,c#,wpf,listbox,checkbox,C#,Wpf,Listbox,Checkbox,我正在使用列表框项目中的复选框,如何从列表中获取所选复选框 <ListBox ItemsSource="{Binding NameList}" HorizontalAlignment="Left" Margin="16,68,0,12" Name="listBox1" Width="156" IsEnabled="True" SelectionMode="Multiple" Focusable="True" IsHitTestVisible="True" IsTextSearchEnab
<ListBox ItemsSource="{Binding NameList}" HorizontalAlignment="Left" Margin="16,68,0,12" Name="listBox1" Width="156" IsEnabled="True" SelectionMode="Multiple" Focusable="True" IsHitTestVisible="True" IsTextSearchEnabled="False" FontSize="12" Padding="5" SelectionChanged="listBox1_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox Content="{Binding Path=CNames}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
有这样的模板吗
<ListBox.ItemTemplate>
<DataTemplate>
........
<CheckBox Content=""
IsChecked="{Binding IsSelected, Mode=TwoWay,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type ListViewItem}}}" />
..........
<!-- Use Other UIElements to Show your Data -->
有这样的模板吗
<ListBox.ItemTemplate>
<DataTemplate>
........
<CheckBox Content=""
IsChecked="{Binding IsSelected, Mode=TwoWay,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type ListViewItem}}}" />
..........
<!-- Use Other UIElements to Show your Data -->
您可以将这些项中每个项的数据上下文从UI中移开,并创建一个可观察的对象集合
public ObservableCollection<CheckedItem> List { get;set;}
public class CheckedItem : INotifyPropertyChanged
{
private bool selected;
private string description;
public bool Selected
{
get { return selected; }
set
{
selected = value;
OnPropertyChanged("Selected");
}
}
public string Description
{
get { return description; }
set
{
description = value;
OnPropertyChanged("Description");
}
}
/* INotifyPropertyChanged implementation */
}
然后在您的列表框ItemTemplate中
<ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=Selected}"
Content={Binding Path=Description}" />
</DataTemplate>
</ItemTemplate>
您选择的项目现在可在ObservableCollection中使用,而不是在UI元素中循环您可以将这些项目的数据上下文移离UI,并创建对象的ObservableCollection
public ObservableCollection<CheckedItem> List { get;set;}
public class CheckedItem : INotifyPropertyChanged
{
private bool selected;
private string description;
public bool Selected
{
get { return selected; }
set
{
selected = value;
OnPropertyChanged("Selected");
}
}
public string Description
{
get { return description; }
set
{
description = value;
OnPropertyChanged("Description");
}
}
/* INotifyPropertyChanged implementation */
}
然后在您的列表框ItemTemplate中
<ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=Selected}"
Content={Binding Path=Description}" />
</DataTemplate>
</ItemTemplate>
您选择的项目现在可以在ObservableCollection中使用,而不是通过UI元素循环使用
private void save_Click(object sender, RoutedEventArgs e)
{
foreach (CheckBox item in list1.Items)
{
if (item.IsChecked)
{
MessageBox.Show(item.Content.ToString());
}
}
}
我建议使用以下代码:
private void save_Click(object sender, RoutedEventArgs e)
{
foreach (CheckBox item in list1.Items)
{
if (item.IsChecked)
{
MessageBox.Show(item.Content.ToString());
}
}
}
什么是异常,从哪一行抛出异常?什么是异常,从哪一行抛出异常?这就是我在过去+1中实现的方法。一个小问题:如果需要双向绑定,您可能希望在CheckedItem上实现INotifyPropertyChanged。同意INotifyPropertyChanged。这确实取决于您的要求。这就是我在过去+1中实现相同功能的方式。一个小问题:如果需要双向绑定,您可能希望在CheckedItem上实现INotifyPropertyChanged。同意INotifyPropertyChanged。这确实取决于你的要求