0

I have an exceed data grid and I want to bind context menu property of column header. Here is my code

<UserControl
             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:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid" x:Class="Sedan.Ux.Sheets.Views.SheetView" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <BooleanToVisibilityConverter x:Key="VisibilityConverter"/>
        <Style TargetType="{x:Type xcdg:ColumnManagerCell}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type xcdg:ColumnManagerCell}">

                        <Grid VerticalAlignment="Center" HorizontalAlignment="Stretch">

                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <TextBlock Grid.Column="0" Text="{TemplateBinding Content}" HorizontalAlignment="Stretch"/>
                            <TextBlock Grid.Column="1" Text=" &#x25BC;" HorizontalAlignment="Stretch" >

                            </TextBlock>

                        </Grid>

                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="ContextMenu">
                <Setter.Value>
                    <ContextMenu DataContext="{Binding Menus}" ItemsSource="{Binding Menus}">
                        <ContextMenu.ItemContainerStyle>
                            <Style TargetType="MenuItem">
                                <Setter Property="Header" Value="{Binding Name}"/>
                                <Setter Property="ItemsSource" Value="{Binding SubMenus}"/>
                                <Setter Property="Command" Value="{Binding Command}"/>
                                <Setter Property="CommandParameter" Value="{Binding Name}"></Setter>
                            </Style>
                        </ContextMenu.ItemContainerStyle>
                    </ContextMenu>
                </Setter.Value>
            </Setter>

        </Style>

    </UserControl.Resources>
     <Grid>
        <xcdg:DataGridControl  Grid.Row="0" x:Name="Sheet" HorizontalAlignment="Stretch"  VerticalAlignment="Stretch" Margin="3" AutoCreateColumns="True" ReadOnly="{Binding ReadOnly}" ItemsSource="{Binding Data}" SelectionMode="Single" SelectedItem="{Binding SelectedItem}">

        </xcdg:DataGridControl>

    </Grid>



</UserControl>

I am not able to bind Menus property when I am using Setter in style.

If I remove the binding and hard code menu items ,I am able to see the context menu.

I have done research on how to get it done and I found various suggestions on stackoverflow and other forums regarding using 'Relative Source Self' in binding but that didnt work for me.

If I bind Menus to context menu of any button in user control (not via style) , then I am able to see menu. Please help.

Smarth Behl
  • 103
  • 6
  • *I have done research on how to get it done and I found various suggestions on stackoverflow and other forums regarding using 'Relative Source Self' in binding but that didnt work for me*... then you just didn't understand it. Rather than repeating all of these details yet again on this website, please see my answer to the [Add context menu in datagrid, how to get the select Item value](http://stackoverflow.com/questions/18610905/add-context-menu-in-datagrid-how-to-get-the-select-item-value/18611364#18611364) question for a full solution. – Sheridan Apr 24 '14 at 14:22
  • @Sheridan I tried that but it didnt work, this is what my code looks like after doing changes, I am not sure about userControl in ancestor Type.I am new to WPF and I am not sure if I can use data template in exceed data grid for columncellmanager Can you Please help My code is at pastebin http://pastebin.com/tXFuECk7 – Smarth Behl Apr 24 '14 at 15:32
  • First, understand the `ContextMenu`... get that working in a nice simple project with just a `ContextMenu` on a `TextBox` or something. Once you understand it, then you can try to attach it to your column header. You're trying to work on two problems at once and it's clearly confusing you. – Sheridan Apr 24 '14 at 15:44
  • I have tried ContextMenu using a simple button and it works. The code is here : http://pastebin.com/ExWRB52S Same code is working for button without tag property but it doesnt work for columncellmanager – Smarth Behl Apr 24 '14 at 16:19
  • You haven't data bound the `ContextMenu.DataContext` to the `PlacementTarget.Tag` like in all the online examples. You also need to data bind the `UserControl.DataContext` to the `Tag` property *of the placement target*... the element with the `ContextMenu` set on it. Anyway, I'm not going over all this with you any further because this is a duplicate question and should be closed. There are many online solutions for this including the linked answer... you just haven't read them or followed them correctly. – Sheridan Apr 24 '14 at 22:14

0 Answers0