locked
Context menu items binding RRS feed

  • Question

  • Hi. I'm very new to WPF and i'm using context menu like this in my app :

      <ContextMenu>
                        <MenuItem Header="First Menu Item" />
                        <MenuItem Header="Second Menu Item" />
     </ContextMenu>

    i want to bind header dynamically from my sql database.

    how can i do it ?

    Thanks.

    Thursday, September 26, 2013 5:52 AM

Answers

  • bind the contextmenu.itemssource to your data returned from your database and then set the containerstyle to bind to the object property you want for header.

    <ContextMenu ItemsSource="{Binding MyList}">
    <ContextMenu.ItemContainerStyle>
    <Style TargetType="MenuItem">
    <Setter Property="Header" Value="{Binding HeaderProperty}" />
    </Style>
    </ContextMenu.ItemContainerStyle>
    </ContestMenu>

    andy
    • Proposed as answer by Joba Diniz Friday, September 27, 2013 1:53 AM
    • Marked as answer by Leo (Apple) Yang Wednesday, October 2, 2013 9:41 AM
    Thursday, September 26, 2013 3:57 PM

All replies

  • Here is a nice article how you can reach that:

    http://www.codeproject.com/Tips/362436/Data-binding-in-WPF-DataGrid-control

    Thursday, September 26, 2013 6:51 AM
  • bind the contextmenu.itemssource to your data returned from your database and then set the containerstyle to bind to the object property you want for header.

    <ContextMenu ItemsSource="{Binding MyList}">
    <ContextMenu.ItemContainerStyle>
    <Style TargetType="MenuItem">
    <Setter Property="Header" Value="{Binding HeaderProperty}" />
    </Style>
    </ContextMenu.ItemContainerStyle>
    </ContestMenu>

    andy
    • Proposed as answer by Joba Diniz Friday, September 27, 2013 1:53 AM
    • Marked as answer by Leo (Apple) Yang Wednesday, October 2, 2013 9:41 AM
    Thursday, September 26, 2013 3:57 PM