I am looking on how to add a ContextMenu on the header of my GridView.
I don't want to add it on the Column's header, but on the full "line" where are all my headers. So even if the user hides all columns, the tooltip will still be available.
Concretely, I want to be able by right-clicking in the headers line, to make appear a ToolTip, containing a list of Comboboxes, corresponding to which columns I want to see or hide.
For now I only found how to launch an event on right-click on header (GridViewColumnHeader.MouseRightButtonDown), but then I have no idea how to go further.
You can use the GridView.ColumnHeaderContainerStyle to set the GridViewColumnHeader.ContextMenu property:
<GridView>
<GridView.ColumnHeaderContainerStyle>
<Style TargetType="GridViewColumnHeader">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem>
<MenuItem.Header>
<ComboBox />
</MenuItem.Header>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</GridView.ColumnHeaderContainerStyle>
</GridView>
Related
How to iterate through the column header template of a WPF data grid ?
I want to get the checkbox's content from the data grid's column header template.
My header template is below:
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<CheckBox x:Name="chkbxHeader" Content="{Binding}"
Loaded="chkbxHeader_Loaded" Unchecked="chkbxHeader_Unchecked"
Checked="chkbxHeader_Checked" IsChecked="True" Width="100" Height="23" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.Resources>
I want to know which are the checkboxes are checked and unchecked.
By default, all the check box in the grid's column header must be in checked status. I tried to get the content of each checkbox in the loaded event. But the content was not ready at that time.
I have a need to bind context menu items to a list in my view model. It works and the code is rather simple, but there is one problem that my users won't accept. You have to click in a certain area of the context menu in order to get the command to trigger.
If you click outside of the darker blue area then the command does not trigger.
The XAML is as follows:
<ContextMenu ItemsSource="{Binding MyContextMenuItems}">
<ContextMenu.ItemTemplate>
<DataTemplate>
<MenuItem Header="{Binding Header}" Command="{Binding Command}" />
</DataTemplate>
</ContextMenu.ItemTemplate>
</ContextMenu>
It appears that the dark blue is the actual MenuItem control, but I need the parent to be the clickable area, I would also prefer that it was all highlighted the same, but I think That that won't be very difficult to figure out once I know the solution to the main issue.
Thanks to DRapp's comment I figured out the solution to my issue. The menu item is actually created automatically and I was creating an menu item inside of the one that was created for me. The solution was to define the ContextMenu.ItemContainerStyle instead of using the ContextMenu.ItemTemplate. Below is the final XMAL.
<ContextMenu ItemsSource="{Binding MyContextMenuItems}">
<ContextMenu.ItemContainerStyle>
<Style TargetType="{x:Type MenuItem}">
<Setter Property="Header" Value="{Binding Header}" />
<Setter Property="Command" Value="{Binding Command}" />
</Style>
</ContextMenu.ItemContainerStyle>
</ContextMenu>
Basically, I want to show a context menu when the user right clicks a valid item on my data grid, however, all the answers I've found are made to do this with a data grid view, which I can't use since I am working on a WPF project. To be clear, I know how to deal with the context menu when right clicking, however, I need the menu only to popup when the user right clicks a row in the data grid. Please help me c:
<DataGrid.Resources>
<Style TargetType="DataGridRow">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<Menu>
<MenuItem Header="Cut"/>
</Menu>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</DataGrid.Resources>
You can try this to create ContextMenu.
<DataGrid Name="dGrid">
<DataGrid.ContextMenu>
<ContextMenu >
<MenuItem Header="Click Here" />
</ContextMenu>
</DataGrid.ContextMenu>
</DataGrid>
I have some contextMenu which shows list of object.
My problem is when one of the objects starts with underline ("_") for example : _obj1,
and the results is that the mentitem displayed without the underline : obj1".
Any idea?
I fix it by changing the header template.
<Style TargetType="{x:Type MenuItem}">
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding Header, RelativeSource= {RelativeSource Mode=FindAncestor, AncestorType={x:Type MenuItem}}}" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
I think you may miss a feature provided by wpf when using a underline-start name.
When you use _obj1, you get the menutime displayed as obj1, but you should try press ALT then you will see the obj1 "underlined" and you can use ALT+o (the first character of the obj name) to "press" the menuitem without using a Mouse.
For example, when you write this:
<MenuItem Header="_File">
<MenuItem .../>
</MenuItem>
Then you just see the Menu with "File", but you can use ALT+F as you are clicking this MenuItem.
Just try this and you shall understand.
I am currently having a ContextMenu on a ListView with its view style set to "GridView". However, this gives me trouble because you are able to right-click the visual columns in the top of the ListView to get the context-menu to appear as well.
I only want the context menu to appear on all the items in the list, and I would hate to program a method to add a new context-menu for every list or so.
Is there a smart way of doing this? Maybe through a template of some sort? Which approach would be the best one?
You can create a style that will be applied to the items in the list view as opposed to the list view itself. Something like:
<ListView>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="Send Email" />
<MenuItem Header="Delete" />
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
</ListView>
For reference, I couldn't get it to work like this, as it kept resulting in the error:
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
Additional information: Cannot add content of type 'System.Windows.Controls.ContextMenu' to an object of type 'System.Object'. Error at object 'System.Windows.Controls.ContextMenu' in markup file '<file>'.
Instead I had to use a static resource, which while having the exact same result, actually worked. Go figure.
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="ContextMenu" Value="{StaticResource ListViewContextMenu}" />
</Style>
</ListView.ItemContainerStyle>
<Application.Resources>
<ContextMenu x:Key="ListViewContextMenu">
<MenuItem Header="Play" />
</ContextMenu>
</Application.Resources>