简体   繁体   English

Mvvm-Light用户控制RelayCommand模板绑定

[英]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="&#xE141;" Foreground="Green"></RadioButton>
    <RadioButton Style="{StaticResource NavRadioButtonStyle}" Tag="&#xE7E6;" Foreground="Green"></RadioButton>
    <RadioButton Style="{StaticResource NavRadioButtonStyle}" Tag="&#xEB52;" 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

  • My first Approach was to set the Command Property in xaml and to implement the method in the viewmodel (eg MainViewModel ) which actually worked - shorten xaml: <RadioButton Command="{Binding Command}"></RadioButton> 我的第一种方法是在xaml中设置Command属性,并在实际起作用的viewmodel(例如MainViewModel )中实现该方法-缩短xaml: <RadioButton Command="{Binding Command}"></RadioButton>
  • Because I wanted to set the Propery in the Page using the customcontrol like this <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: 对于将RadioButtonCommand连接到PinCommandTileToolBar ,您可以在用户控件中使用如下示例代码:

<RadioButton Tag="&#xEB52;" Foreground="Green" Command="{x:Bind PinCommand, Mode=OneWay}"></RadioButton> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM