[英]Mvvm-Light User Control RelayCommand TemplateBinding
[UWP - Windows 10]
[UWP-Windows 10]
I'm new to MVVM-Light and so I got some starter issues. 我是MVVM-Light的新手,所以遇到了一些入门问题。 I created a custom Usercontrol which is called
TileToolbar
and is containing this xaml: 我创建了一个名为
TileToolbar
的自定义用户TileToolbar
,其中包含以下xaml:
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<RadioButton Style="{StaticResource NavRadioButtonStyle}" Tag="" Foreground="Green"></RadioButton>
<RadioButton Style="{StaticResource NavRadioButtonStyle}" Tag="" Foreground="Green"></RadioButton>
<RadioButton Style="{StaticResource NavRadioButtonStyle}" Tag="" Foreground="Green"></RadioButton>
</StackPanel>
Now I want to add a RelayCommand
for each RadioButton and I want each Page which is containing the custom usercontrol to be able to bind a custom RelayCommand
. 现在,我想为每个RadioButton添加一个
RelayCommand
,并且希望每个包含自定义usercontrol的页面都能够绑定自定义RelayCommand
。
Command
Property in xaml and to implement the method in the viewmodel (eg MainViewModel
) which actually worked - shorten xaml: <RadioButton Command="{Binding Command}"></RadioButton>
Command
属性,并在实际起作用的viewmodel(例如MainViewModel
)中实现该方法-缩短xaml: <RadioButton Command="{Binding Command}"></RadioButton>
<TileToolbar PinCommand={Binding Command}></TileToolbar>
I created a dependency property of type RelayCommand but the TemplateBinding didn't work. <TileToolbar PinCommand={Binding Command}></TileToolbar>
这样的customcontrol在页面中设置属性,所以我创建了RelayCommand类型的依赖项属性,但是TemplateBinding无法正常工作。 So my question: How would I create a property like PinCommand
of type RelayCommand
in the UserControl so I can later bind to it in xaml for example on the Mainpage
? 所以我的问题是:我该如何在UserControl中创建诸如
PinCommand
类型的PinCommand
之类的RelayCommand
,以便以后可以在xaml中将其绑定到例如Mainpage
?
So my question: How would I create a property like PinCommand of type RelayCommand in the UserControl so I can later bind to it in xaml for example on the Mainpage?
所以我的问题是:我如何在UserControl中创建诸如RelayCommand类型的PinCommand之类的属性,以便以后可以在xaml中将其绑定到例如Mainpage?
You can register a PinCommand
in the type of RelayCommand
in your UserControl
's code behind for example like this: 你可以注册一个
PinCommand
在类型RelayCommand
在你的UserControl
的代码背后例如是这样的:
public static DependencyProperty PinCommandProperty = DependencyProperty.Register("PinCommand", typeof(RelayCommand), typeof(TileToolbar), new PropertyMetadata(null));
public RelayCommand PinCommand
{
get
{
return (RelayCommand)GetValue(PinCommandProperty);
}
set
{
SetValue(PinCommandProperty, value);
}
}
Now you can use this TileToolbar
in your MainPage
for example like this: 现在,您可以在
MainPage
使用此TileToolbar
,例如:
<Controls:TileToolbar Grid.Row="1" VerticalAlignment="Bottom" PinCommand="{Binding pinCommand, Mode=OneWay}" />
Code in view model is like this: 视图模型中的代码如下所示:
private RelayCommand _pinCommand;
public RelayCommand pinCommand
{
get
{
if (_pinCommand == null)
{
_pinCommand = new RelayCommand(() =>
{
//TODO:
},
() => true);
}
return _pinCommand;
}
}
And for the work of connecting the Command
of RadioButton
to the PinCommand
of TileToolBar
, you can in your user control for example code like this: 对于将
RadioButton
的Command
连接到PinCommand
的TileToolBar
,您可以在用户控件中使用如下示例代码:
<RadioButton Tag="" Foreground="Green" Command="{x:Bind PinCommand, Mode=OneWay}"></RadioButton>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.