Skip to content

How to apply a vertical border when left click the context menu? #2

Description

@Siyumii

I am currently implementing a WPF project. There I have a datagrid, In one of the columns in this data grid,In one of the columns in this data grid, user should be able to open a context menu for both right click and left click. I have implemented both of these scenarios, but when user left click and open the context menu, It doesn't show as how it's displayed for the right click.

When user right click for the very first time context menu display correctly

  • Right click for the very first time - display correctly

![When right click for the very first time it display as this]
(https://user-images.githubusercontent.com/16635100/89280647-897e8580-d666-11ea-8cad-3cfbd6803a60.png)

When user left click for the very first time, border and icon is missing

  • Left click for the very first time - border and icon is missing

  • then right click - border and icon is missing
    When user left click for the very first time, border(highlighted in blue color) and icon (highlighted in red color)is missing

Please note that what I meant by border here is, In the context menu in left side there's a grey color vertical line can be seen. That line cannot seen when I left click the menu. I tried several ways to implement this but none of them were successful. I have all the styles I have tried

<!-- ContextMenu Styles -->
    <Style x:Key="RowStyle" TargetType="{x:Type DataGridRow}">
        <Setter Property="ContextMenu" Value="{DynamicResource RowContextMenu}" />
    </Style>

    <Style x:Key="ContextMenuStyle" TargetType="{x:Type ContextMenu}">
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ContextMenu}">
                    <Border BorderThickness="1" x:Name="Border" BorderBrush="{StaticResource StandardBorderColor}" >
                        <StackPanel ClipToBounds="True" Orientation="Vertical" IsItemsHost="True"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter TargetName="Border" Property="Background" Value="{DynamicResource VsBrush.Window}" />
                            <Setter Property="Foreground" Value="{DynamicResource VsBrush.WindowText}"/>
                            <Setter Property="Background" Value="{DynamicResource VsBrush.Window}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

<!--Style 1-->
    <Style x:Key="VerticalSeparatorStyle"
       TargetType="{x:Type Separator}"
       BasedOn="{StaticResource {x:Type Separator}}">
        <Setter Property="Margin" Value="20,2,1,20"/>
        <Setter Property="BorderBrush" Value="GreenYellow"/>
        <Setter Property="LayoutTransform">
            <Setter.Value>
                <TransformGroup>
                    <TransformGroup.Children>
                        <TransformCollection>
                            <RotateTransform Angle="90"/>
                        </TransformCollection>
                    </TransformGroup.Children>
                </TransformGroup>
            </Setter.Value>
        </Setter>
    </Style>

<!--Style 2-->
    <Style x:Key="FocusVisualStyle" TargetType="{x:Type ContextMenu}">
        <Setter Property="BorderBrush" Value="LightSeaGreen" />
        --><!--<Setter Property="SnapsToDevicePixels" Value="True" />--><!--
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ContextMenu}">
                    <StackPanel Orientation="Horizontal" IsItemsHost="True" ClipToBounds="True">
                        <Line StrokeThickness="1" X1="100" Y1="0" X2="100" Y2="{Binding ElementName=ContextMenu, Path=ActualHeight}" />
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>-->
   
<!--Style 3-->

    <Style TargetType="{x:Type ContextMenu}">
        <Setter Property="FocusVisualStyle">
            <Setter.Value>
                <Style>
                    <Setter Property="Control.Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <Rectangle StrokeThickness="12" Stroke="red" />
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </Setter.Value>
        </Setter>
    </Style>

<!--Style 4-->
    <Style x:Key="{x:Static MenuItem.SeparatorStyleKey}"
       TargetType="Separator">
        <Setter Property="Width" Value="100"/>
        <Setter Property="Margin" Value="20,2,1,20"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Separator">
                    <Border BorderBrush="{StaticResource StandardBorderColor}" Opacity="0.3"
                BorderThickness="1" VerticalAlignment="Stretch" ClipToBounds="True"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
   
<!--End of Styles-->



Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions