0

我编写了一个自定义ICommand实现,可作为静态属性使用:

public class GridViewCommands
{
    public GridViewCommands()
    {

    }

    /// <summary>
    /// Toggle Selection-Command
    /// </summary>
    public static ICommand ToggleSelection
    {
        get
        {
            return new GridViewToggleSelectionCommand();
        }
    }
}

我尝试将此属性绑定到一个简单的Button-Command

<ui:GridViewControl x:Name="gridView" Grid.Row="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" />

<Button HorizontalAlignment="Left" Width="100" Margin="220,0,0,0" Content="Toggle" x:Name="toggleButton" Command="{x:Static  ui:GridViewCommands.ToggleSelection}" CommandParameter="{Binding ElementName=gridView}"></Button>

但是如果我启动我的应用程序,方法 in 中的parameter-Parameter始终为空。我的目标是传递一个实例作为命令参数。CanExecuteGridViewToggleSelectionCommandGridViewControl

我在这里做错了什么:ui:GridViewCommands.ToggleSelection}" CommandParameter="{Binding ElementName=gridView"}

编辑

绑定不会引发任何异常。

非常感谢!

4

2 回答 2

0

我认为您正在尝试这样做:

public static ICommand ToggleSelection { get { return new RelayCommand<GridViewControl>(GridViewToggleSelectionCommand); } }
private static void GridViewToggleSelectionCommand(GridViewControl theControl)
{
    // do something with the control here
}

我看到您已将其标记为 mvvm...这不是 mvvm。您的视图模型不应该对视图一无所知。我也无法理解为什么您将 ToggleSelection 设置为静态,这意味着您的命令处理程序也必须是静态的。

老实说,如果你继续走你正在走的路,你就是在为一个痛苦的世界做好准备。您的 GridViewControl 应该绑定到您的视图模型中的一个对象,并且您应该通过您的 ICommand 处理程序将这个对象传递回视图模型。

于 2015-08-12T22:54:01.820 回答
0

您实际上可以在单击按钮时获取参数。

于 2015-08-13T11:35:05.250 回答