簡體   English   中英

ContextMenu上的DataContext綁定

[英]DataContext binding on ContextMenu

我有一個帶有ContextMenuDataGrid ,我試圖找出如何正確綁定上下文的方法。

到目前為止,我已經閱讀了上下文菜單,它取代了可視樹,因此DataContext是不同的。 考慮到這一點,提供的解決方案是使用屬性Tag ,但是我仍然無法使其工作:

<UserControl>
    <!--#region DataGrid-->

    <DataGrid ItemsSource="{Binding Model.Collection}"
              Tag="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}, Path=DataContext}">

        <!--#region Resources-->

        <DataGrid.Resources>

            <!--#region DataGridCell-->

            <Style TargetType="DataGridCell">

                <Setter Property="ContextMenu">
                    <Setter.Value>
                        <ContextMenu DataContext="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
                            <MenuItem Header="Open Details"
                                      Command="{Binding DataContext.OpenRowDetailsCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
                                      CommandParameter="{Binding DataContext.SelectedIndex, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
                        </ContextMenu>
                    </Setter.Value>
                </Setter>

            </Style>

            <!--#endregion DataGridCell-->

        </DataGrid.Resources>

        <!--#endregion Resources-->

        <!--#region DataGridColumns-->

        <DataGrid.Columns>
            <DataGridTextColumn Header="Filename"
                                Binding="{Binding FileInfo.Name}"
                                Width="Auto" />
        </DataGrid.Columns>

        <!--#endregion DataGridColumns-->

    </DataGrid>

    <!--#endregion DataGrid-->

UserControlDataContext運行正常,因為我還有其他以這種方式使用DataContext命令。

任何人看到任何錯誤或有其他方法嗎?

提前致謝。

只要在父UserControl的視圖模型類中定義了OpenRowDetailsCommandSelectedIndex屬性,這應該可以工作:

<Style TargetType="DataGridCell">
    <Setter Property="Tag" Value="{Binding RelativeSource={RelativeSource AncestorType=UserControl}}" />
    <Setter Property="ContextMenu">
        <Setter.Value>
            <ContextMenu>
                <MenuItem Header="Open Details"
                          Command="{Binding PlacementTarget.Tag.DataContext.OpenRowDetailsCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
                          CommandParameter="{Binding PlacementTarget.Tag.DataContext.SelectedIndex, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
            </ContextMenu>
        </Setter.Value>
    </Setter>
</Style>

我喜歡為此使用BindingProxy (如本SO回答所述

public class BindingProxy : Freezable
{
    protected override Freezable CreateInstanceCore()
    {
        return new BindingProxy();
    }

    public object Data
    {
        get { return (object)GetValue(DataProperty); }
        set { SetValue(DataProperty, value); }
    }

    public static readonly DependencyProperty DataProperty =
        DependencyProperty.Register("Data", typeof(object),typeof(BindingProxy));
}

像這樣使用它:

<DataGrid ItemsSource="{Binding Model.Collection}">
    <DataGrid.Resources>
        <local:BindingProxy x:Key="VMProxy" Data="{Binding}" />
        <local:BindingProxy x:Key="DataGridProxy" Data="{Binding RelativeSource={RelativeSource Self}}" />
        <Style TargetType="DataGridCell">
            <Setter Property="ContextMenu">
                <Setter.Value>
                    <ContextMenu>
                        <MenuItem Header="Open Details"
                                  Command="{Binding Data.OpenRowDetailsCommand, Source={StaticResource VMProxy}}" 
                                  CommandParameter="{Binding Data.SelectedIndex, Source={StaticResource DataGridProxy}}"/>
                    </ContextMenu>
                </Setter.Value>
            </Setter>
        </Style>
    </DataGrid.Resources>

暫無
暫無

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

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