在动态复选框列表中获取选定的项目WPF C#
在我的ViewModel中,我有一个类别的集合(类别具有ID和名称)。 我设法在视图中动态显示了我的复选框列表。 Thoses复选框用于过滤一些数据(仅显示具有检查类别的数据)。 如何获得检查类别?
<ListBox x:Name="listViewCategories" Grid.Row="1" ItemsSource="{Binding Categories}" SelectedItem="{Binding SelectedItems}" BorderBrush="Transparent" SelectionMode="Multiple" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Background="{x:Null}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Name}"
Margin="3"
IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}, Path=IsSelected}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
这是我的模型,
private ObservableCollection<Category> _selectedItems = new();
public ObservableCollection<Category> SelectedItems
{
get => _selectedItems;
set => SetProperty(ref _selectedItems, value, () => Console.WriteLine("Click"));
}
我尝试了很多事情。
- 在类别中创建一个属性,并将签名键绑定到该属性(实现InotifyPropertychanged的类别),但它不起作用。
- 将复选框的“签名”绑定到ListBoxItem的“签发属性”,但我的SelecteItems集合从未被通知任何变化。
感谢您的帮助!
In my viewmodel, I have a Collection of Categories (a Category has an id and a name).
I managed to dynamically display my list of checkboxes in my view. Thoses checkboxes are used to filter some data (displaying only data that have the checked categories).
How can I get the checked categories?
<ListBox x:Name="listViewCategories" Grid.Row="1" ItemsSource="{Binding Categories}" SelectedItem="{Binding SelectedItems}" BorderBrush="Transparent" SelectionMode="Multiple" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Background="{x:Null}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Name}"
Margin="3"
IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}, Path=IsSelected}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
And here is my model
private ObservableCollection<Category> _selectedItems = new();
public ObservableCollection<Category> SelectedItems
{
get => _selectedItems;
set => SetProperty(ref _selectedItems, value, () => Console.WriteLine("Click"));
}
I have tried a lot of things.
- creating a property in Category 'IsChecked' and bind IsChecked of the checkbok to that property (Category implementing INotifyPropertyChanged) but it doesn't work.
- Binding 'IsChecked' of the checkbox to the IsSelected property of the ListBoxItem but my SelectedItems collection is never notified of any changement.
Thanks for you help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想有多种方法可以解决这个问题。在下面,您找到了我的路。
我建议使用
microsoft.xaml.behaviors.wpf.dll
可以通过Nuget下载。其中包含&lt;互动。triggers&gt;
,可用于将事件映射到命令。我还提供了一个listView来显示所选类别。
请参阅以下代码。
这是XAML的应用程序:
这是我的类别类别:
这是我的ViewModel类:
我还实现了一个
relayCommand
,它很简单,很简单,但是大多数MVVM-Frameworks已经具有类似的东西在商店中。这是RelayCommand类:
因此,基本上我有一个方法addCategory和RemoveCategory(在这种情况下为lambda,但您明白了),并通过EventTocommand将其绑定到ViewModel。
I guess there are multiple ways to approach this. Down below you find my way.
I would suggest using the
Microsoft.Xaml.Behaviors.Wpf.dll
which can be downloaded via Nuget. This contains<Interaction.Triggers>
which can be used to map a event to an command.I have also included a listview to show the selected categories.
See the following code.
This is the application XAML:
This is my category class:
This is my viewmodel class:
I also implemented a class
RelayCommand
which was easy and pretty straight forward but most of the MVVM-Frameworks have something like this already in store.this is the RelayCommand Class:
So basically I had a method AddCategory and RemoveCategory (inline in this case as lambda but you get the point) and bound this via EventToCommand to the viewmodel.