我正在做一些简单的应用程序重构以跟随MVVM,我的问题是如何将SelectionChanged事件从我的代码中移出到viewModel?我已经看了一些绑定元素到命令的例子,但是并没有完全掌握它.谁能帮忙解决这个问题.谢谢!
有人可以使用下面的代码提供解决方案吗?非常感谢!
public partial class MyAppView : Window
{
public MyAppView()
{
InitializeComponent();
this.DataContext = new MyAppViewModel ();
// Insert code required on object creation below this point.
}
private void contactsList_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
//TODO: Add event handler implementation here.
//for each selected contact get the labels and put in collection
ObservableCollection<AggregatedLabelModel> contactListLabels = new ObservableCollection<AggregatedLabelModel>();
foreach (ContactListModel contactList in contactsList.SelectedItems)
{
foreach (AggregatedLabelModel aggLabel in contactList.AggLabels)
{
contactListLabels.Add(aggLabel);
}
}
//aggregate the contactListLabels by name
ListCollectionView selectedLabelsView …Run Code Online (Sandbox Code Playgroud) 我最近在WPF中编程很多,但我的View和ViewModel在这一点上并不是分开的.嗯,这是部分原因.我的所有绑定都与文本框中的文本,标签内容,数据网格中的列表,......相关,都是由常规属性完成的,其中包含NotifyPropertyChanged事件.
处理按钮点击或文本更改内容的所有事件都是通过链接事件来完成的.现在,我想开始使用命令并找到这篇文章:http://www.codeproject.com/Articles/126249/MVVM-Pattern-in-WPF-A-Simple-Tutorial-for-Absolute.它解释了如何设置MVVM,但我对它感到困惑RelayCommand.
它做了什么工作?它对我表单中的所有命令都可用吗?如果(a)某些文本框未填写,如何禁用该按钮?
编辑1:
对"我的表单中的所有命令都可用吗?"的一个很好的解释.在这里回答:https://stackoverflow.com/a/22286816/3357699
这是我到目前为止的代码:https://stackoverflow.com/a/22289358/3357699
任何人都可以告诉我实际的语法是什么EventToCommand.据我所知,EventToCommand该类适用于Silverlight/WPF和WP7,因此我认为它是一个更好的选择.
据我所知,我可以添加任何点击事件并将其强行插入我的ViewModel,但我在找到最佳方法时遇到问题.
我知道你可以在没有Blend的情况下添加它,但是有可用的片段吗?
或者是否有更简单的方法通过VS 2010添加它?任何帮助或如果有人知道这方面的好教程将是伟大的.
对于那些使用纯MVVM的人来说,如何在不回复代码的情况下处理ComboBox SelectionChanged事件?
我尝试了例如AttachedBehaviors,但不支持Event ="SelectedChanged":
<ComboBox>
<ComboBoxItem Content="Test1">
<c:CommandBehaviorCollection.Behaviors>
<c:BehaviorBinding Event="SelectionChanged"
Command="{Binding SelectedChanged}"
CommandParameter="MainBorder123"/>
</c:CommandBehaviorCollection.Behaviors>
</ComboBoxItem>
<ComboBoxItem Content="Test2"/>
<ComboBoxItem Content="Test3"/>
</ComboBox>
Run Code Online (Sandbox Code Playgroud) WPF Datagrid有两种选择模式,Single或Extended.WPF ListView有第三个 - 多个.此模式允许您单击并选择多行而不按住CTRL或Shift.任何人都知道如何为datagrid做这个?
我有一个文本框KeyUp事件触发器连接到WPF中的命令.我需要传递作为命令参数按下的实际键.
该命令执行正常,但处理它的代码需要知道按下的实际键(记住这可能是一个输入键或任何不仅仅是一个字母,所以我无法从TextBox.text获取它).
无法弄清楚如何做到这一点.XAML:
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
Run Code Online (Sandbox Code Playgroud)
XAML:
<TextBox Height="23" Name="TextBoxSelectionSearch" Width="148" Tag="Enter Selection Name" Text="{Binding Path=SelectionEditorFilter.SelectionNameFilter,UpdateSourceTrigger=PropertyChanged}" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyUp">
<i:InvokeCommandAction Command="{Binding SelectionEditorSelectionNameFilterKeyUpCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
Run Code Online (Sandbox Code Playgroud) 我想用 ViewModel 绑定一个事件。
我用了
clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity
我使用触发器相同
<Canvas Grid.Row="2" Grid.Column="2" x:Name="InteractiveCanvas" Style="{StaticResource canvasChartStyle}" ClipToBounds="True" >
<intr:Interaction.Triggers>
<intr:EventTrigger EventName="MouseEnter">
<intr:InvokeCommandAction Command="AppointmentEditing" />
</intr:EventTrigger>
</intr:Interaction.Triggers>
</Canvas>
Run Code Online (Sandbox Code Playgroud)
但我需要使用事件参数。这里无法得到相同的结果。
在 wpf 中,有没有可能绑定事件并获取事件参数?不使用 MVVM lite 或 PRISM。
我只想获取事件参数
我有一个下拉列表(ComboBox),显示计算机上可用的所有COM端口.现在,当您连接和断开设备时,端口会进出.
出于性能原因,我不想继续通话System.IO.Ports.SerialPort.GetPortNames(),而只是在用户点击Combobox时调用它?这可能吗?是否有MVVM方法来解决这个问题?
wpf ×7
mvvm ×6
c# ×5
.net ×1
binding ×1
combobox ×1
events ×1
mvvm-light ×1
relaycommand ×1
selection ×1
wpfdatagrid ×1
wpftoolkit ×1
xaml ×1