我使用Mvvm / Prism设计界面,我创建了一个后面有代码的用户控件,它有标签文本框boutton(没有mvvm)
整个项目都在mvvm模式上,我有一个包含3个用户控件的Home页面。
在我的userControl中,我将标签内容数据绑定到属性(字符串)PROP1文本框内容(字符串)PROP2到属性(代码隐藏),在我的HOME中我只是将这些属性绑定到我的HomeViewModel中的属性
<local:userControl PROP1="{Binding Text}" PROP2="{Binding Name}" />
现在我想用相同的方法对按钮点击进行数据绑定,在userControl页面中想要todo是
<Button Content="Button" Command="{Binding Klick}"/>
但我不知道如何将它存放在财产中,以便我以后可以使用
以下是我想要的最终主页类似
<local:userControl Klick="{Binding Commandp}" PROP1="{Binding Textp}" PROP2="{Binding Namep}" />
和HomeViewModel
class HomeViewModel : BindableBase
{
public ICommand Commandp{ get; private set; }
private readonly IRegionManager _regionManager;
public HomeViewModel (IRegionManager regionManager)
{
Commandp= new DelegateCommand(() => NavigateTo("Docs"));
}
private void NavigateTo(string url)
{
_regionManager.RequestNavigate(Regions.Contentregoin, url);
}
}
PS:我正在使用ViewModelLocator.AutoWireViewModel
答案 0 :(得分:1)
这与PROP1
和PROP2
的工作方式相同 - 创建一个依赖属性,你就可以了。
public static readonly DependencyProperty KlickProperty = DependencyProperty.Register( nameof(Klick), typeof(ICommand), typeof(userControl), new PropertyMetadata( default(ICommand) ) );
public ICommand Klick
{
get { return (ICommand)GetValue( KlickProperty ); }
set { SetValue( KlickProperty, value ); }
}