DisplayMemberPath for data binding not working

9 posts, 1 answers
  1. Murray
    Murray avatar
    13 posts
    Member since:
    Jul 2010

    Posted 27 May 2012 Link to this post

    Hi 

    I am trying to implement a RadContextMenu and am not seeming to be able to get the DisplayMemberPath to work

    The following code does not display the correct label on the menuitem ( the menu items do display)

    <telerik:RadButton cal:Message.Attach="Show" BorderBrush="{x:Null}" Background="{x:Null}"
                            Foreground="Black" FontFamily="Arial" Margin="1,3">
                         <Grid>
                             <telerik:RadContextMenu.ContextMenu>
                                 <telerik:RadContextMenu  ItemsSource="{Binding Items}"
    Placement="Right" DisplayMemberPath="DisplayName"   / >
                             
                             </telerik:RadContextMenu.ContextMenu>
                             
                             <TextBlock VerticalAlignment="Top"
                                         Foreground="Red"
                                         HorizontalAlignment="Right"
                                         Margin="2"
                                         Text="{Binding Status}" />
     
             <StackPanel>
     
                                
                                 <Image Source="{Binding Icon}" HorizontalAlignment="Center" VerticalAlignment="Center" />
               <TextBlock Text="{Binding IconName}" Foreground="White" HorizontalAlignment="Center" />
     
             </StackPanel>
                         
                         </Grid>
      
         </telerik:RadButton>

    But this code does

    <telerik:RadContextMenu.ContextMenu>
                                        <telerik:RadContextMenu  ItemsSource="{Binding Items}" Placement="Right" DisplayMemberPath="DisplayName"    >
                                            <telerik:RadContextMenu.ItemTemplate>
                                                <DataTemplate>
                                                  
                                                    <TextBlock Text="{Binding DisplayName}"  />                                
                                                </DataTemplate>                            
                                            </telerik:RadContextMenu.ItemTemplate>
                                                                                 
                                        </telerik:RadContextMenu>
                                    </telerik:RadContextMenu.ContextMenu>


    I don't want to use the second code I would like to use DisplayMemberPath.  I also tried setting ToString() on the buisness object as stated in the documenation (http://www.telerik.com/help/wpf/radcontextmenu-populating-with-data-data-binding-support-overview.html ) But this did not work either.

    Does anyone have any idea why this isn't working?

    thanks

    Murray


  2. Dani
    Admin
    Dani avatar
    848 posts

    Posted 28 May 2012 Link to this post

    Hi Murray,

    You should simply set the ContextMenu property of the RadButton itself, rather than that of the Grid which is content of the RadButton:

    <telerik:RadButton Height="50" VerticalAlignment="Center">
                <telerik:RadContextMenu.ContextMenu>
                    <telerik:RadContextMenu ItemsSource="{Binding}" DisplayMemberPath="DisplayName" />
                </telerik:RadContextMenu.ContextMenu>
                  
                <Grid>
                    <TextBlock VerticalAlignment="Top"
                                         Foreground="Red"
                                         HorizontalAlignment="Right"
                                         Margin="2"
                                         Text="Status" />
                </Grid>         
            </telerik:RadButton>

    The second approach can not work because the ItemTemplate and the DisplayMemberPath properties are set simultaneously, which is not possible.

    Hope this helps.

    Kind regards,
    Dani
    the Telerik team
    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
  3. UI for WPF is Visual Studio 2017 Ready
  4. Murray
    Murray avatar
    13 posts
    Member since:
    Jul 2010

    Posted 28 May 2012 Link to this post

    Hi Dani

    Yes you are right about the ItemTemplate and the DisplayMemberPath being set at once being wrong. This was an error I made while writing the post. It works because it seems to be ignoring the DisplayMemberPath.

    I moved it to the button. Which is a better place. But this also makes no difference. I am still getting the object path not the text of the "DisplayName" being shown ( ie My.Project.ViewModel)

    The code below is the corrected code which should work but does not.

    thanks for your time

    regards

    Murray
    <telerik:RadButton cal:Message.Attach="Show" BorderBrush="{x:Null}" Background="{x:Null}"
                                   Foreground="Black" FontFamily="Arial" Margin="1,3">
                  <telerik:RadContextMenu.ContextMenu>
                    <telerik:RadContextMenu ItemsSource="{Binding Items}" Placement="Right"
                                            DisplayMemberPath="DisplayName" />
                  </telerik:RadContextMenu.ContextMenu>
                  <Grid>
     
                    <TextBlock VerticalAlignment="Top" Foreground="Red" HorizontalAlignment="Right"
                               Margin="2" Text="{Binding Status}" />
     
                    <StackPanel>
     
     
                      <Image Source="{Binding Icon}" HorizontalAlignment="Center"
                             VerticalAlignment="Center" />
                      <TextBlock Text="{Binding IconName}" Foreground="White" HorizontalAlignment="Center" />
     
                    </StackPanel>
     
                  </Grid>
     
                </telerik:RadButton>
  5. Dani
    Admin
    Dani avatar
    848 posts

    Posted 28 May 2012 Link to this post

    Hi Murray,

    I tested your scenario with a basic sample project. I believe it works just as expected. Please, check the attached solution.


    Kind regards,
    Dani
    the Telerik team
    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
  6. Murray
    Murray avatar
    13 posts
    Member since:
    Jul 2010

    Posted 28 May 2012 Link to this post

    Hi Dani

    I have found the bug it is to do with theming

    please modify this code in your solutions

    public MainWindow()
            {
                StyleManager.ApplicationTheme = new Expression_DarkTheme();
                InitializeComponent();
     
                List<Data> items = new List<Data>();
                for (int i = 0; i < 5; i++)
                {
                    items.Add(new Data() { ID = i, DisplayName = "Item " + i });
                }
     
                this.DataContext = items;
            }


    As you will now see, when set to Expresssio_DarkTheme the DisplayMemberPath stops working

    regards

    Murray
  7. Answer
    Dani
    Admin
    Dani avatar
    848 posts

    Posted 29 May 2012 Link to this post

    Hello,

    I can confirm that this is a bug in the Expression Dark theme in particular. This bug will be fixed for the official release. Meanwhile, you can use the following workaround: in the SubMenuItemTemplate of RadMenuItemStyle add the following to the ContentControl x:Name="Header":
    ContentTemplateSelector="{TemplateBinding HeaderTemplateSelector}"


    All the best,
    Dani
    the Telerik team
    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
  8. Murray
    Murray avatar
    13 posts
    Member since:
    Jul 2010

    Posted 29 May 2012 Link to this post

    Hi Dani

    Could you please give me an example of how to do that

    regards

    Murray
  9. Dani
    Admin
    Dani avatar
    848 posts

    Posted 29 May 2012 Link to this post

    Hello Murray,

    Attached is an illustrative sample. Note that all four templates for RadMenuItem include the ContentTemplateSelector property where discussed.

    If you are using an application theme via the StyleManager, this is the way to go. However, please, keep in mind that we have now improved our support for implicit styling and themes are also applied implicitly as an alternative to the Telerik StyleManager. Since at some point in the future, the StyleManager will be dropped, I can suggest that you keep in mind updating your project with implicit styling in the place of the application theme. More details on the implicit styling can be found here.

    Hope this helps.

    All the best,
    Dani
    the Telerik team
    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
  10. Murray
    Murray avatar
    13 posts
    Member since:
    Jul 2010

    Posted 29 May 2012 Link to this post

    Hi Dani

    Thanks for that it worked fine.

    Interesting to hear about style manager being removed. I am currently using style manager because it is quick to apply a style while we are in the development phase of the application. Implicit styles are more powerful, but they also take more work :)

    thanks for your help

    regards

    Murray
Back to Top
UI for WPF is Visual Studio 2017 Ready