MVVM 中的 WPF 绑定检查事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13603175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF binding checked event in MVVM
提问by AndreaG
I'm new to MVVM pattern, I must intercept the checked/unchecked operation inside a the following view
我是 MVVM 模式的新手,我必须在以下视图中拦截选中/未选中的操作
SendMessageView.xaml
发送消息视图.xaml
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<CheckBox Checked="Specialita_Checked"
Unchecked="Specialita_Unchecked"
Content="{Binding Path=Item.Name}"
IsChecked="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type ListBoxItem}},
Path=IsSelected,Mode=TwoWay}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
SendMessageView.xaml.cs
SendMessageView.xaml.cs
private void Specialita_Checked(object sender, System.Windows.RoutedEventArgs e)
{
var aSendMessageViewModel = (SendMessageViewModel)this.DataContext;
if (aSendMessageViewModel != null)
{
var aCheckBox = (CheckBox)sender;
aSendMessageViewModel.AddSpecialita(aCheckBox.Content.ToString());
}
}
A file called aSendMessageViewModel.csis call inside a SendMessageView.xaml.csand this is not correct.
Help me in correctly using MVVM pattern.
aSendMessageViewModel.cs调用的文件是在 a 内调用SendMessageView.xaml.cs,这是不正确的。帮助我正确使用 MVVM 模式。
回答by Louis Kottmann
Add the System.Windows.Interactivity.dll in your project and reference it at the top of your xaml (xmlns:i="...")
在您的项目中添加 System.Windows.Interactivity.dll 并在您的 xaml (xmlns:i="...") 顶部引用它
Then you can do:
然后你可以这样做:
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<CheckBox Content="{Binding Path=Item.Name}"
IsChecked="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type ListBoxItem}},
Path=IsSelected,Mode=TwoWay}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding DataContext.OnCheckedCommand, ElementName=myCtrl}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</CheckBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
And use a command named OnCheckedCommand in your ViewModel to handle it.
并在您的 ViewModel 中使用名为 OnCheckedCommand 的命令来处理它。
回答by Kek
Why not just write
为什么不直接写
aSendMessageViewModel.AddSpecialita(aCheckBox.Content.ToString());
in the IsSelected setter of your ListBoxItem datacontext ?
在 ListBoxItem 数据上下文的 IsSelected 设置器中?
EDIT
编辑
What will happen when you check your check box is your SelectedItem of your listBox will change (due to IsSelected on ListViewItem). Then you could do something in the setter of the SlectedItem of DataContext of your listBox
当您选中复选框时会发生什么,您的 listBox 的 SelectedItem 将发生变化(由于 ListViewItem 上的 IsSelected)。然后你可以在你的 listBox 的 DataContext 的 SlectedItem 的 setter 中做一些事情
public MyObject SelectedItem
{
get { return _selItem; }
set
{
if(_selItem != value)
{
if(_selItem != null)
_selItem.OnUncheck();
_selItem = value;
_selItem.OnCheck();
NotifyPropertyChange("SelectedItem");
}
}
}

