我已经尝试了很长一段时间来搜索这个并且没有找到有帮助的结果。 (也许我的google-foo需要工作?) 我也是WPF MVVM的新手,所以我还在学习很多东西。
我的问题实际上分为两部分...... 首先,我有一个Combobox,在View中有一个Checkbox。 代码:
<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");
}
}
这里有什么问题?
我的第二个问题是: 解决上述问题后,我需要将所有已检查的名称(在这种情况下为人员名称)附加到列表中,并将值传递回单独的视图/视图模型。
我完全失去了如何实现这一目标。 任何帮助或建议将不胜感激。
谢谢:)
答案 0 :(得分:0)
可能出错的两件事
您是否尝试在命令中设置复选框值 - &#34; DataContext.CheckBoxSelected&#34;?由于您已经有一个绑定,这可能会重置该值。
单击该复选框不会更新组合框中的选定项目,只会更新复选框。