c# – 将Action注入UserControl

我的视图包含一个Button和一个UserControl. UserControl还包含一个Button.我的View中的Button1和UserControl中的Button2都应该在ViewModel中调用相同的方法.

因此我需要在用户控件中“注入”(不知道如何调用它)RelayCommand.我认为依赖属性会很容易,但我无法让它工作.

我尝试了什么:

UserControl包含依赖项属性,该属性在包含UserControl的View的XAML代码中设置:

用户控制代码:

public const string LoadDataCommandPropertyName = "LoadDataCommand";
public Action LoadDataCommand
{
    get
    {
        return (Action)GetValue(LoadDataCommandProperty);
    }
    set
    {
        SetValue(LoadDataCommandProperty, value);
    }
}

public static readonly DependencyProperty LoadDataCommandProperty = DependencyProperty.Register(
    LoadDataCommandPropertyName,
    typeof(Action),
    typeof(GridViewPersistenceController),
    new UIPropertyMetadata());

查看Xaml代码:(在UserControl中使用dp)

<customControls:MyUserControl Grid.Column="1" 
                              x:Name="RadGridViewSettingsPersistenceControl" 
                              GridControl="{Binding ElementName=RadGridView}"
                              LoadDataCommand="{Binding ActionTest}"
                              />

UserControl中的LoadDataCommand绑定到ViewModel中的此属性

private const string ActionTestPropertyName = "ActionTest";
private Action actionTest;
public Action ActionTest
{
    get
    {
        return this.actionTest;
    }

    set
    {
        this.RaisePropertyChanging(ActionTestPropertyName);
        this.actionTest = value;
        this.RaisePropertyChanged(ActionTestPropertyName);
    }
}

ActionTest属性在ViewModel的构造函数中初始化

public MyViewModel()
        {
            this.ActionTest = new Action(this.LoadData);
        }

visual studio输出窗口也给了我一个绑定错误(我不明白)

System.Windows.Data Error: 40 : BindingExpression path error:
‘ActionTest’ property not found on ‘object’ ”MyUserControl’
(Name=’RadGridViewSettingsPersistenceControl’)’.
BindingExpression:Path=ActionTest; DataItem=’MyUserControl’
(Name=’RadGridViewSettingsPersistenceControl’); target element is
‘MyUserControl’ (Name=’RadGridViewSettingsPersistenceControl’); target
property is ‘LoadDataCommand’ (type ‘Action’)

我找到了一个有效的解决方法,但我不喜欢它.我在View codebehind的LoadedEvent中设置了LoadDataCommand.它看起来很乱,我觉得我错过了一些重要的概念.

//TODO: Dirty coding, fix me
    private void MyView_OnLoaded(object sender, RoutedEventArgs e)
    {
        this.RadGridViewSettingsPersistenceControl.LoadDataCommand = new Action((this.DataContext as MyViewModel).LoadData);
    }

问题:

>如何使用View中的xaml代码将ViewModel中定义的Command / Action传递给UserControl?
>为什么我目前使用绑定的方法不起作用?
>我错过了一些基本概念吗? (我可以通过代表更轻松地实现这一目标吗?(我试过……))

最佳答案
从错误中可以看出:

‘ActionTest’ property not found on ‘object’ ”MyUserControl’
(Name=’RadGridViewSettingsPersistenceControl’)’

绑定引擎正在搜索控件中的属性,而不是在ViewModel中. (默认情况下,它在DataContext控件中搜索属性,我怀疑你已经在代码中的某个地方设置了控件的DataContext)

使用RelativeSource获取UserControl的DataContext,它将是您的ViewModel.

LoadDataCommand="{Binding DataContext.ActionTest,
                   RelativeSource={RelativeSource Mode=FindAncestor,
                                           AncestorType=UserControl}}"

此外,不是创建Action类型的DP,而是使用ICommand并在ViewModel中创建ICommand并绑定到它.

转载注明原文:c# – 将Action注入UserControl - 代码日志