[英]MVVM WPF ComboBox with CheckBox
I've tried to search for this for quite some time and haven't found results that help. 我已经尝试搜索了很长时间,但没有找到有用的结果。 (perhaps my google-foo needs work?) I'm also fairly new to WPF MVVM, so there is a lot I'm still learning.
(也许我的google-foo需要工作吗?)我对WPF MVVM也很陌生,所以我还在学习很多东西。
My Question is actually in two parts... Firstly, I have a Combobox that has Checkboxes within them within a View. 我的问题实际上分为两部分...首先,我有一个组合框,其中的一个视图中包含复选框。 Code:
码:
<ComboBox Grid.Column="0"
Grid.ColumnSpan="5"
Margin="15,0,0,0"
ItemsSource="{Binding StaffNames}"
SelectedValue="{Binding SelectedStaffNames}"
Grid.Row="4">
<ComboBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Path=FullName}"
VerticalAlignment="Center"
IsChecked="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ComboBoxItem}}, Path=IsSelected,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"
Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window},
Path=DataContext.CheckBoxSelected}"
Margin="3">
</CheckBox>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Everytime I check a checkbox, it seems that the checkboxes get automatically unchecked. 每次我选中一个复选框时,似乎这些复选框都会自动取消选中。
My ViewModel code is as follows. 我的ViewModel代码如下。
private bool _isSelected;
public bool IsSelected
{
get
{
return _isSelected;
}
set
{
_isSelected = value;
RaisePropertyChanged("IsSelected");
}
}
What is wrong here? 怎么了
My second question is: Once the above is resolved, I need to append all checked names (staff names in this case) to a list and pass the values back to a separate view/view model. 我的第二个问题是:解决以上问题后,我需要将所有选中的名称(在这种情况下为工作人员名称)附加到列表中,并将值传递回单独的视图/视图模型。
I'm at a complete loss for how to accomplish this. 我对如何做到这一点完全不知所措。 Any help or suggestions would be greatly appreciated.
任何帮助或建议,将不胜感激。
Thanks :) 谢谢 :)
2 things that could be going wrong 2件可能出错的事情
Are you trying to set the check box value in your command - "DataContext.CheckBoxSelected"? 您是否在尝试在命令“ DataContext.CheckBoxSelected”中设置复选框值? Since you already have a binding, this could be resetting the value.
由于您已经具有绑定,因此可以重置该值。
Clicking the checkbox does not update selected item in combo box but only updates the checkbox. 单击复选框不会更新组合框中的所选项目,而只会更新复选框。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.