'Custom ContextMenu Command in Datagrid's Textbox from ViewModel
I have a UserControl which includes Datagrid. Inside Datagrid I've set EditingElementStyle with Textboxes that have ContextMenu for Copy/Paste commands. I need to implement custom command for Copy/Paste, preferably command should be executed from ViewModel.
This is my UserControl with datagrid:
<UserControl x:Class="My_project.View.SearchEmployees"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:ctl="clr-namespace:My_project.Controls"
x:Name="_search">
<Grid>
//... Other controls
<ctl:My_dataGrid ItemsSource="{Binding Employees}">
<DataGrid.Columns>
<DataGridTextColumn Width="130" Header="Name" Binding="{Binding NAME}">
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="TextBox" BasedOn="{StaticResource Txt_dgv}">
<Setter Property="Tag" Value="{Binding DataContext,
ElementName=_search}"/> <!--doesn't work-->
</Style>
</DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</ctl:My_dataGrid>
</Grid>
</UserControl>
My custom Textbox with ContextMenu:
<Style TargetType="{x:Type TextBox}" x:Key="Txt_dgv">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Grid>
<Border BorderBrush="Silver" BorderThickness="0,0,0,1"
Background="Transparent" SnapsToDevicePixels="True">
<ScrollViewer x:Name="PART_ContentHost" Focusable="False"
HorizontalScrollBarVisibility="Hidden"
VerticalAlignment="Center"
VerticalScrollBarVisibility="Hidden" />
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu >
<MenuItem Command="{Binding Path=PlacementTarget.Tag.COPY_COMMAND,
RelativeSource={RelativeSource AncestorType=ContextMenu}}" <!--doesn't work-->
Header="Copy" />
<MenuItem Command="{Binding Path=PlacementTarget.Tag.PASTE_COMMAND,
RelativeSource={RelativeSource AncestorType=ContextMenu}}" <!--doesn't work-->
Header="Paste" />
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
In example above I used Textbox Tag property to set DataContext of UserControl, and then set that to DataContext of ContextMenu.
But output is nothing - not even errors. I'm guessing that this has to be linked with fact that ContextMenu is not a part of Visual Tree, and that Textbox is too deep inside it.
Anyone has a better idea on how to solve this?
Solution 1:[1]
You cannot bind to _search
using the ElementName
property since it belongs to a different XAML namescope.
You could bind to the UserControl
's DataContext
using the RelativeSource
property though:
<Setter Property="Tag" Value="{Binding DataContext,
RelativeSource={RelativeSource AncestorType=UserControl}}"/>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | mm8 |