我有一个里面有按钮的UserControl。这个按钮需要添加一些项目到网格里面,UC说。我知道我可以通过Click事件做到这一点。
这里的问题是,我使用的是MVVM,在它们相应的ViewModel之外修改数据会破坏格式(可以这么说)。
有没有办法创建一个ICommand依赖属性,这样我就可以将DP绑定到按钮上,并具有将项目添加到我的ViewModel中的网格的功能?(我的UC和ViewModel中都已有该列表,它们的工作情况与预期一致)
谢谢。
发布于 2015-02-22 23:52:12
找到了我想要的方法来解决这个问题。把答案留在这里,这样人们就可以使用它:
1)在用户控件的代码隐藏中,创建依赖项属性。我选择ICommand,因为在我的ViewModel中,我将其设置为DelegateCommmand:
public static readonly DependencyProperty CommandProperty =
DependencyProperty.Register(
"Command",
typeof(ICommand),
typeof(UserControl));
public ICommand Command
{
get
{
return (ICommand)GetValue(CommandProperty);
}
set
{
SetValue(CommandProperty, value);
}
}2)在UserControl的XAML代码中,绑定这个依赖属性(本例中是一个按钮):
<Grid DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}">
<Button Command="{Binding Command}" />
</Grid>3)接下来,在您的ViewModel上声明一个命令属性并进行相应的配置:
public ICommand ViewModelCommand { get; set; }
public ViewModelConstructor()
{
ViewModelCommand = new DelegateCommand(ViewModelCommandExecute);
}
private void ViewModelCommandExecute()
{
// Do something
}4)最后,在嵌套UserControl的视图上,我们声明绑定:
<UserControls:UserControl Command={Binding ViewModelCommand}/>这样,绑定就会发生,你可以从任何用户控件的按钮绑定命令到你的ViewModels,而不会破坏MVVM。
发布于 2015-02-22 18:43:08
基本方法是创建一个实现ICommand的对象(即MyCommand),并将其嵌套在您的ViewModel中。在MyCommand中,你不能访问你的ViewModel。你可以解决这个问题(例如在MyCommand构造函数中传递一个对ViewModel的引用),但是最后它的代码太多了(对于像这样的简单东西)。我认为几乎没有人真正做到这一点。
大多数人使用DelegateCommand来解决(大多数)上述问题。
最后但并非最不重要的一点是,仅使用事件处理程序。
如果您简单地像这样编写它们:
void Grid_MouseMove(object sender, MouseEventArgs e)
{ viewModel.SaveMousePosition(e.GetPosition()); }您没有违反任何MVVM规则。
而且你不能用命令来处理上面的事件。
没有针对MouseMove的命令(对于大多数事件都没有),并且不能在命令中传递事件参数。
您可以像this一样使用Interaction.Triggers处理每个事件
但是您仍然无法处理事件参数(并添加难看的XAML)。
对我来说,直到WPF支持事件处理程序中的数据绑定,比如
Grid MouseMove="{Binding SaveMousePosition(e)}"代码隐藏仍然是处理事件的最有效方法。
发布于 2019-03-04 19:32:49
我遇到了类似的问题,这个问题/答案对我帮助最大;所以我会在这里发布我的解决方案,以防其他人稍后会用谷歌搜索它。由mvvm light制作。
我有一个自定义的winforms控件作为Model,一个WPF控件作为View。所以,xaml of View (我的View有一个用户控件,没有app.xaml):
<UserControl.Resources>
<ResourceDictionary>
<viewModel:ViewModelLocator x:Key="Locator" />
</ResourceDictionary>
</UserControl.Resources>
<UserControl.DataContext>
<Binding Path = "Main" Source="{StaticResource Locator}"></Binding>
</UserControl.DataContext>
<Grid>
<Button Command="{Binding Zoom, ElementName=Wrapper}"></Button>
<viewModel:ProfileWrapper x:Name="Wrapper" >
</viewModel:ProfileWrapper>
</Grid>点击按钮被路由到ProfileWrapper中的RelayCommand缩放(这就是我的模型实现的地方)
那么ProfileWrapper的xaml就很简单了:
<Grid>
<WindowsFormsHost>
<local:ManualControl x:Name="abc" ></local:ManualControl>
</WindowsFormsHost>
</Grid>以及ProfileWrapper的代码幕后:
public partial class ProfileWrapper : UserControl
{
public ProfileWrapper()
{
InitializeComponent();
test = abc;
Command = new RelayCommand(() => test.bZoomIn());
}
public ManualControl test;
public RelayCommand Zoom { get; set; }
public static readonly DependencyProperty CommandProperty =
DependencyProperty.Register(
"Zoom",
typeof(ICommand),
typeof(ProfileWrapper));
public ICommand Command
{
get
{
return (ICommand)GetValue(CommandProperty);
}
set
{
SetValue(CommandProperty, value);
}
}
}我的MainViewModel类是空的,所有的函数都转到了ProfileWrapper类,这可能很糟糕,但至少它可以工作。
https://stackoverflow.com/questions/28650304
复制相似问题