0

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?

3
  • There is no element _search that you can bind to in the EditingElementStyle. Commented Jan 6, 2022 at 20:50
  • @mm8 - why, EditingElementStyle is also not a part of Visual Tree ? Is there anything else I could try ? Commented Jan 7, 2022 at 6:59
  • See my answer please. Commented Jan 7, 2022 at 12:08

1 Answer 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}}"/>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.