[英]MVVM WPF ComboBox with CheckBox
我已經嘗試搜索了很長時間,但沒有找到有用的結果。 (也許我的google-foo需要工作嗎?)我對WPF MVVM也很陌生,所以我還在學習很多東西。
我的問題實際上分為兩部分...首先,我有一個組合框,其中的一個視圖中包含復選框。 碼:
<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");
}
}
怎么了
我的第二個問題是:解決以上問題后,我需要將所有選中的名稱(在這種情況下為工作人員名稱)附加到列表中,並將值傳遞回單獨的視圖/視圖模型。
我對如何做到這一點完全不知所措。 任何幫助或建議,將不勝感激。
謝謝 :)
2件可能出錯的事情
您是否在嘗試在命令“ DataContext.CheckBoxSelected”中設置復選框值? 由於您已經具有綁定,因此可以重置該值。
單擊復選框不會更新組合框中的所選項目,而只會更新復選框。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.