我试过搜索这个很长一段时间,并没有找到有帮助的结果。 (也许我的google-foo需要工作?) 我对WPF MVVM也是比较新的,所以还有很多我仍然在学习。带有复选框的MVVM WPF组合框
我的问题实际上分为两部分...... 首先,我有一个组合框,它们在一个视图内有复选框。 代码:
<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>
每次我选中一个复选框,似乎得到复选框自动取消选中。
我的ViewModel代码如下。
private bool _isSelected;
public bool IsSelected
{
get
{
return _isSelected;
}
set
{
_isSelected = value;
RaisePropertyChanged("IsSelected");
}
}
这里有什么问题?
我的第二个问题是: 一旦解决了上述问题,我需要将所有选中的名称(本例中为职员名称)附加到列表中,并将值传递回单独的视图/视图模型。
我完全丧失了如何做到这一点。 任何帮助或建议将不胜感激。
谢谢:)可能会错误
你为什么指定'RelativeSource'? DataContext被传递给每个孩子。输出中是否有绑定错误? – 2013-02-21 19:51:34
到目前为止没有绑定错误。 RelativeSource被添加为“事后”,试图获取传递给ViewModel的值......这似乎不起作用。它似乎产生相同的事情有或没有命令条目 – Xyphius 2013-02-21 20:08:15
'{Binding RelativeSource = {RelativeSource FindAncestor,AncestorType = {x:Type ComboBoxItem}},Path = IsSelected,UpdateSourceTrigger = PropertyChanged,Mode = TwoWay}'所以IsChecked为true仅当选择ComboBoxItem时?这是没有意义的。我认为你的控制设计有点怪异。听起来就像你应该使用一个ListBox来检查绑定到你的模型。 – Will 2013-02-21 20:46:14