5

我在理解命令参数绑定的工作原理时遇到了一些麻烦。

当我在调用 InitializeComponent 之前创建小部件类的实例时,它似乎工作正常。对 ExecuteCommand 函数中的参数(Widget)的修改将被“应用”到 _widget。这是我所期望的行为。

如果 _widget 的实例是在 InitializeComponent 之后创建的,我会在 ExecuteCommand 函数中得到 e.Parameter 的空引用异常。

为什么是这样?如何使用 MVP 模式进行这项工作,在创建视图后可能会创建绑定对象?

public partial class WidgetView : Window
{
    RoutedCommand _doSomethingCommand = new RoutedCommand();

    Widget _widget;

    public WidgetView()
    {
        _widget = new Widget();
        InitializeComponent();
        this.CommandBindings.Add(new CommandBinding(DoSomethingCommand, ExecuteCommand, CanExecuteCommand));
    }

    public Widget TestWidget
    {
        get { return _widget; }
        set { _widget = value; }
    }

    public RoutedCommand DoSomethingCommand
    {
        get { return _doSomethingCommand; }
    }

    private static void CanExecuteCommand(object sender, CanExecuteRoutedEventArgs e)
    {
        if (e.Parameter == null)
            e.CanExecute = true;
        else
        {
            e.CanExecute = ((Widget)e.Parameter).Count < 2;
        }
    }

    private static void ExecuteCommand(object sender, ExecutedRoutedEventArgs e)
    {
        ((Widget)e.Parameter).DoSomething();
    }
}



<Window x:Class="CommandParameterTest.WidgetView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WidgetView" Height="300" Width="300"
    DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <StackPanel>
        <Button Name="_Button" Command="{Binding DoSomethingCommand}"
             CommandParameter="{Binding TestWidget}">Do Something</Button>
    </StackPanel>
</Window>


public class Widget
{
    public int Count = 0;
    public void DoSomething()
    {
        Count++;
    }
}
4

2 回答 2

4

InitializeCompenent 处理与文件关联的 xaml。正是在这个时间点,首先处理 CommandParameter 绑定。如果您在 InitializeCompenent 之前初始化您的字段,那么您的属性将不会为空。如果您在之后创建它,则它为空。

如果要在 InitializeCompenent 之后创建小部件,则需要使用依赖属性。依赖属性将引发一个通知,该通知将导致 CommandParameter 被更新,因此它不会为空。

下面是如何使 TestWidget 成为依赖属性的示例。

public static readonly DependencyProperty TestWidgetProperty =
    DependencyProperty.Register("TestWidget", typeof(Widget), typeof(Window1), new UIPropertyMetadata(null));
public Widget TestWidget
{
    get { return (Widget) GetValue(TestWidgetProperty); }
    set { SetValue(TestWidgetProperty, value); }
}
于 2008-11-02T22:37:34.603 回答
0

即使使用依赖属性,您仍然需要调用 CommandManager.InvalidateRequerySuggested 来强制执行正在评估的命令的 CanExecute。

于 2008-12-22T06:00:24.383 回答