简体   繁体   中英

WPF DatGrid Context Menu Binding

I have a WPF DataGrid with a DataContext that is bound to an Observable collection of view models.

My DataGrid columns are each bound to a different property of the objects in the collection and this works correctly, displaying the objects in the collection on the grid.

I want to add a context menu onto this grid, with the menu items defined as another property on the view model that make up the overall observable collection. These menu items are another Observable collection of menu items. I know this approach can work as I have similar code on a treeview in another part of the application.

I have defined the binding of the DataGrid and ContextMenu as follows:

 <DataGrid Name="SynchErrors" Grid.Row="1"
              Style="{StaticResource SortableGrid}"
              ItemsSource="{Binding}">
        <DataGrid.Resources>
            <userControls:BindingProxy x:Key="BindingProxy" DataContextProxy="{Binding}"  />

        </DataGrid.Resources>
        <DataGrid.ContextMenu>
             <ContextMenu DataContext="{Binding Path=DataContextProxy, Mode=TwoWay, Source={StaticResource BindingProxy}}"
            ItemsSource="{Binding Path=DataContextProxy.ContextMenuCommandList, Mode=TwoWay, Source={StaticResource BindingProxy}}">
                <ContextMenu.Resources>
                    <Style TargetType="{x:Type MenuItem}">
                        <Setter Property="Command" Value="{Binding ContextMenuCommand}"></Setter>
                        <Setter Property="Header" Value="{Binding DisplayName}"></Setter>
                    </Style>
                </ContextMenu.Resources>
            </ContextMenu>
        </DataGrid.ContextMenu>

        <DataGrid.Columns>
            <DataGridTextColumn Header="From"
                                MinWidth="120"
                                Width="*"
                                Binding="{Binding Path=Owner}"/>

            <DataGridTextColumn Header="Subject"
                                Width="2*"
                                Binding="{Binding Path=Name}"
                                SortMemberPath="Name"/>
       <DataGrid.Columns>

When I run my application, I get my items listed correctly in the grid, but when I right click, I get an empty context menu and the following binding error:

System.Windows.Data Error: 40 : BindingExpression path error: 'ContextMenuCommandList' property not found on 'object' ''ObservableCollection`1' (HashCode=53690177)'. BindingExpression:Path=DataContextProxy.ContextMenuCommandList; DataItem='BindingProxy' (HashCode=43857660); target element is 'ContextMenu' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')

I have used the BindingProxy to pass the DataContext from the DataGrid to the ContextMenu as the context menu is outside the Visual Tree, however this appears to be passing the entire collection rather than the individual items bound to each row.

Is it possible to define the binding of a context menu on a DataGrid to reference each individual item of the parent collection?

您可以尝试将此ItemsSource =“ {Binding Path = DataContextProxy.ContextMenuCommandList更改为此ItemsSource =” {Binding Path = DataContextProxy / ContextMenuCommandList

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM