Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1240
Setting the Label Presenter’s Context Menu Property
posted

An earlier post on the old forum suggested that a context menu could be assigned to a field/column header by setting the style on the Label Presenter.  I tried this via the following code.  The code in question is in red.

 

---------------------------------------------------------------------------------------------------------------------------

// Create and assign a field layout

FieldLayout theFieldLayout = new FieldLayout();

FieldLayouts.Add(theFieldLayout);

 

// Go through all column details

foreach (ColDetails CurrColDetails in m_ColHeaders)

{

    // Create the new field (Column)

    Field currColField = new Field();

 

    // Assign the label presenter

    currColField.Label = CurrColDetails.HeaderName;

 

    // Bind

    currColField.Name = CurrColDetails.Binding;

 

    // Add the new menu item

    MenuItem CurrColMenuItem = new MenuItem();

    CurrColMenuItem.Header = CurrColDetails.HeaderName;

    CurrColMenuItem.DataContext = CurrColDetails;

    CurrColMenuItem.Click += new RoutedEventHandler(MenuItemClickHandler);

    m_ContextMenu.Items.Add(CurrColMenuItem);

 

    // Add the new field to the field layout

    theFieldLayout.Fields.Add(currColField);

}

 

Style labelPresenterStyle = new Style(typeof(LabelPresenter));

labelPresenterStyle.Setters.Add(new Setter(LabelPresenter.ContextMenuProperty, m_ContextMenu));

foreach (Field currField in theFieldLayout.Fields)

{

    currField.Settings.LabelPresenterStyle = labelPresenterStyle;

}

 

grid.FieldLayouts.Add(theFieldLayout);

---------------------------------------------------------------------------------------------------------------------------

 

This results in an exception being thrown after the grid is initialized, the  source of which I have not been able to determine.  I do know that if I take out the style logic (shown in red) the exception does not get thrown. 

 

Am I using the ContextMenuProperty correctly?  I cannot find any documentation anywhere that describes its use.  Thank you.

Parents
No Data
Reply
  • 4850
    Offline posted

    What exception is being thrown?

    BTW, instead of looping over each Field you can specify this setting once for the FieldLayout or even as a grid level default, e.g.

    theFieldLayout.FieldSettings.LabelPresenterStyle = labelPresenterStyle

    or

    grid.FieldSettings.LabelPresenterStyle = labelPresenterStyle

     

Children