Home Forums WPF controls Xceed DataGrid for WPF Context Menu on different grid elements

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #23084 |

    I am trying to create context menus is c# and associate with the ColumnManagerRow and the RowSelector (ie a different context menu depending if the column header or row selection is under the cursor).

    I can create the ColumnManagerRow one in XAML using:

    <xcdg:DataGridControl.View>
    <xcdg:TableView>
    <xcdg:TableView.FixedHeaders>
    <xcdg:ClearHeadersFooters/>
    <DataTemplate>
    <StackPanel>
    <TextBlock>This is the Header Text</TextBlock>
    <xcdg:ColumnManagerRow>
    <xcdg:ColumnManagerRow.ContextMenu>
    <ContextMenu Name=”cm”>
    <MenuItem Header=”Test Item 1″/>
    <MenuItem Header=”Test Item 2″/>
    </ContextMenu>
    </xcdg:ColumnManagerRow.ContextMenu>
    </xcdg:ColumnManagerRow>
    </StackPanel>
    </DataTemplate>
    </xcdg:TableView.FixedHeaders>
    </xcdg:TableView>
    </xcdg:DataGridControl.View>

    I have not been able to create the C# equivalent – any suggestions?

    Also, we have been unable to determine either the XAML (or equivalent c# code) to add a context menu to the RowSelector.

    Any pointers or help would be appreciated.

    Imported from legacy forums. Posted by Derek (had 4329 views)

    User (Old forums)
    Member
    Post count: 23064

    Concerning the creation of templates in code, you have to use the FrameworkElementFactory class. However, I found that you wouldn’t be able to set the ContextMenu property of your ColumnManagerRow template with it. This class does expose a SetValue method but, apparently, you cannot pass it a Visual or a ContentElement. So you have no choice but to use XAML. Furthermore, the Microsoft documentation discourages the use of the FrameworkElementFactory class:

    “This class is a deprecated way to programmatically create templates, which are subclasses of FrameworkTemplate such as ControlTemplate or DataTemplate; not all of the template functionality is available when you create a template using this class. The recommended way to programmatically create a template is to load XAML from a string or a memory stream using the Load method of the XamlReader class.”

    Imported from legacy forums. Posted by Michel (had 591 views)

    User (Old forums)
    Member
    Post count: 23064

    As for the association of a ContextMenu to a RowSelector, it’s a bit trickier than with most of the other grid’s element. I won’t go into the details of why, but I’ll give you a recipe to make it work. Basically, you have to provide an implicit style for RowSelector where you have to specify both the ContextMenu and the Background properties. Setting the background allows the mouse event to be catched by the RowSelector; by default, the RowSelector Template’s background for all themes (as far as I know) have a null value and the framework’s optimization will therefore ignore them. Or something like this. Setting a Transparent Background on the RowSelector will “correct” this and make the ContextMenu work. The following post will contain an example.

    Imported from legacy forums. Posted by Michel (had 588 views)

    User (Old forums)
    Member
    Post count: 23064

    Here, I declared an implicit style “local” to a DataGridControl:

    <xcdg:DataGridControl ItemsSource=”{Binding Source={StaticResource cvsSongs}}”>
    <xcdg:DataGridControl.Resources>
    <Style TargetType=”xcdg:DataRow”>
    <Setter Property=”xcdg:RowSelectorPane.RowSelectorStyle”>
    <Setter.Value>
    <Style TargetType=”xcdg:RowSelector”>
    <Setter Property=”ContextMenu”
    Value=”{StaticResource contextMenu1}”/>
    <Setter Property=”Background”
    Value=”Transparent”/>
    </Style>
    </Setter.Value>
    </Setter>
    </Style>
    </ResourceDictionary>
    </xcdg:DataGridControl.Resources>

    Imported from legacy forums. Posted by Michel (had 493 views)

    User (Old forums)
    Member
    Post count: 23064

    Thanks for this. I am glad that it was not something very simple.

    Imported from legacy forums. Posted by Derek (had 817 views)

    User (Old forums)
    Member
    Post count: 23064

    OR you could use the ContextMenuOpening event and fill the contents of the menu just before it opens in code behind.

    void ContextMenuOpening(object sender, ContextMenuEventArgs e)
    {
    ContextMenu cm = new ContextMenu();
    if(cm!=null)
    {
    TextBox tb = new TextBox();
    if (dgUPF.CurrentColumn != null)
    {
    Binding b = new Binding(“Title”);
    b.Source = grid.CurrentColumn;

    tb.SetBinding(TextBox.TextProperty, b);
    cm.Items.Add(tb);
    }
    grid.ContextMenu = cm;
    }

    }

    Now you change the Title of the current column in a context menu

    Imported from legacy forums. Posted by MiddleTommy (had 5526 views)

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.