簡體   English   中英

通過帶有綁定的CommandParameter傳遞控件為null

[英]Passing control as CommandParameter with Binding is null

我寫了一個自定義的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>

但是,如果我啟動我的應用程序,則GridViewToggleSelectionCommand CanExecute方法中的parameter -Parameter始終為null。 我的目標是傳遞GridViewControl的實例作為命令參數。

我在做什么錯: ui:GridViewCommands.ToggleSelection}" CommandParameter="{Binding ElementName=gridView"}

編輯

綁定不會引發任何異常。

非常感謝你!

我認為您正在嘗試這樣做:

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處理程序將此對象傳遞回該視圖模型。

當您單擊按鈕時,您實際上可以獲得參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM