How to create custome shaped ContextMenu for wpf datagrid cell

Babu R 81 Reputation points
2020-10-10T15:14:16.257+00:00

hai
I want to create a custom shaped context menu for my data grid cell I tried like this

 <UserControl>
         <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Border x:Name="Border" Grid.Row="1" CornerRadius="5" BorderThickness="1.5" BorderBrush="White"  >
                    <Border.Background>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="#FF5997E2" Offset="0.997"/>
                            <GradientStop Color="#FFE1EEF3"/>
                        </LinearGradientBrush>
                    </Border.Background>
                    <Border.Effect>
                        <DropShadowEffect ShadowDepth="3" BlurRadius="9" Direction="300"/>
                    </Border.Effect>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <TextBlock Name="Textblock" Text="Do You Want to delete ?" Grid.Row="0"  FontSize="16" />
                        <Grid Grid.Row="1">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <TextBlock Name="Btn_Yes" Text="Yes"  MouseDown="Btn_Yes_MouseDown" Grid.Column="0"  FontSize="16" HorizontalAlignment="Center"  />
                            <TextBlock Name="Btn_No" Text="No" MouseDown="Btn_No_MouseDown" Grid.Column="1" FontSize="16" HorizontalAlignment="Center" />
                        </Grid>
                    </Grid>
                </Border>
            </Grid>
    </UserControl>

and added one property called clicked

public partial class IContextMenu : UserControl
{
                public IContextMenu()
                {
                    InitializeComponent();
                }
                public bool Clicked
                {
                    get { return (bool)GetValue(ClickedProperty); }
                    set { SetValue(ClickedProperty, value); }
                }
                public static readonly DependencyProperty ClickedProperty =
                    DependencyProperty.Register("Clicked", typeof(bool), typeof(IContextMenu), new PropertyMetadata(false));
}

And I set a style in resource dictionary

<ResourceDictionary>
        <Style x:Key="Contextmenu" TargetType="{x:Type ContextMenu}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate x:Name="aa" TargetType="{x:Type ContextMenu}" >
                            <mycontrols:IContextMenu Background="Transparent"/>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
</ResourceDictionary>

I Want To bind Clicked Property in my main window please help how to acheive this
And in my mainwindow xaml

Developer technologies Windows Presentation Foundation
{count} votes

2 answers

Sort by: Most helpful
  1. DaisyTian-1203 11,646 Reputation points
    2020-10-12T02:36:51.057+00:00

    I do some update for your code as below shown:
    Part 1: Add below code Mainwindow.xaml
    <local:IContextMenu local:Clicked="True" Style="{StaticResource Contextmenu}"></local:IContextMenu>

    Part 2: The style in resource dictionary
    31513-capture.png


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. DaisyTian-1203 11,646 Reputation points
    2020-10-21T02:52:51.383+00:00

    I updated two parts for your code to implement what you want:
    Part 1: Update the style for Contextmenu in Dictionary.xaml as below shown:
    33907-capture.png

    Part 2: Use <ContextMenu Style="{StaticResource Contextmenu}" /> directly in the DataGrid


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.