.NET Framework Bookmark and Share   
 index > Windows Presentation Foundation (WPF) > Menu Style not getting applied to context menu edit controls
 

Menu Style not getting applied to context menu edit controls

I added my own style for context menu items i created and it works..but when i click on a text box and right click in it the Context menu items for edit(cut/copy/paste) is being shown in the default style(gray back ground and black forecolor). Is there anyway i can make them all consistent?

Zest4Quest
Are you applying the styles through a x:Key reference? I.E. (Style="{StaticResource MyMenuStyle}")

If yes, then make sure that the same key is applied to the Cut/Copy/Paste of the context menu.
DeviantSeev
I applied it as

<Style x:Key="{x:Type ContextMenu}" TargetType="ContextMenu">
and that did not change the context menus that i get when i right click a Grid (infragistics)cell but changed the context menu's i explicitly added...

How can i apply the same style to the Cut/Copy/Paste context menu that the Edit control provides (Grid Cells)?
Thanks in advance...

Zest4Quest
Can you show us the XAML that you wrote for the context menu?
DeviantSeev

The folowing are the styles

<

Style x:Key="{x:Type ContextMenu}" TargetType="ContextMenu">
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Grid.IsSharedSizeScope" Value="true"/>
<Setter Property="HasDropShadow" Value="True"/>
<Setter Property="Template">
<Setter.Value>

<ControlTemplate TargetType="ContextMenu">
<Border x:Name="Border"
Background="{StaticResource MenuBackgroundBrush}"
BorderBrush="{StaticResource SolidBorderBrush}"
BorderThickness="1">

<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" KeyboardNavigation.DirectionalNavigation="Cycle"/>

</Border>

<ControlTemplate.Triggers>

<Trigger Property="HasDropShadow" Value="true">
<Setter TargetName="Border" Property="Padding" Value="0,3,0,3"/>
<Setter TargetName="Border" Property="CornerRadius" Value="4"/>
</Trigger>

</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>

</Style>

<!-- Seperator -->

<Style x:Key="{x:Static MenuItem.SeparatorStyleKey}" TargetType="Separator">

<Setter Property="Height" Value="1"/>
<Setter Property="Margin" Value="0,4,0,4"/>
<Setter Property="Template">
<Setter.Value>

<ControlTemplate TargetType="Separator">
<Border BorderBrush="{StaticResource SolidBorderBrush}" BorderThickness="1"/>
</ControlTemplate>
</Setter.Value>

</Setter>
</Style>

Zest4Quest
sorry about the formatting...
Zest4Quest
I am sorry for not being clear, I meant can we see the xaml for the context menu itself?
DeviantSeev
There is not much on the context menu except the folowing

<

igDP:XamDataGrid.ContextMenu>

<ContextMenu >

<MenuItem Header="Expand All" Command="CMD:MainWindow.ExpandCmd" CommandParameter="True" ></MenuItem>

<MenuItem Header="Collapse All" Command="CMD:MainWindow.ExpandCmd" CommandParameter="False" ></MenuItem>

<MenuItem Header="Save Layout" Command="CMD:MainWindow.SaveLayoutCmd"></MenuItem>

<MenuItem Header="Load Layout" Command="CMD:MainWindow.LoadLayoutCmd"></MenuItem>

</ContextMenu>

</igDP:XamDataGrid.ContextMenu>


This menu works fine..But in the infragistics grid when i click on a cell and right click, the menu displayed (CUt/Copy/Paste) does not have any effect(plain gray background)

Zest4Quest
Zest,

You can try the following:

    <Style x:Key="ContextMenuStyle" TargetType="{x:Type ContextMenu}">
        <Setter Property="Background" Value="Cyan"/>
    </Style>
    
    <ContextMenu x:Key="NewContextMenu" Style="{StaticResource ContextMenuStyle}">
        <ContextMenu.Items>
            <MenuItem Header="Cut" Command="Cut"/>
            <MenuItem Header="Copy" Command="Copy"/>
            <MenuItem Header="Paste" Command="Paste"/>
        </ContextMenu.Items>
    </ContextMenu>

    <Style TargetType="{x:Type TextBox}">
        <Setter Property="ContextMenu" Value="{StaticResource NewContextMenu}"/>
    </Style>

This xaml basically creates a new context menu and assigns it to a TextBox. More likely than not, your grid is using TextBox as the edit field. I tried this for the toolkit datagrid and it works as expected. Just know that this style will apply to all your text boxes. If you want your other textboxes not to have this style then you will need to assign them another, specific pre-defined style.
  • Proposed As Answer byDeviantSeev Thursday, September 24, 2009 6:18 PM
  •  
DeviantSeev

You can use google to search for other answers

Custom Search

More Threads

• Binding property of business class to TreeViewItem
• CustomControl and Skins
• ListView - select one item but click on button in another row
• Orcas March CTP: Error on project properties
• How get click event from button embedded in each item of ListView?
• Binding a Command to act upon an item in a collection
• What is System.Windows.Documents.Glyphs?
• Dynamic UI using style created in XAML
• Problem using Focus() to set focus from ListView...
• Treeview TextSearch