This is a migrated thread and some comments may be shown as answers.

DisplayMemberPath for data binding not working

8 Answers 301 Views
ContextMenu
This is a migrated thread and some comments may be shown as answers.
Murray
Top achievements
Rank 1
Murray asked on 28 May 2012, 04:50 AM
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


8 Answers, 1 is accepted

Sort by
0
Dani
Telerik team
answered on 28 May 2012, 09:40 AM
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 >>
0
Murray
Top achievements
Rank 1
answered on 28 May 2012, 10:13 AM
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>
0
Dani
Telerik team
answered on 28 May 2012, 01:57 PM
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 >>
0
Murray
Top achievements
Rank 1
answered on 29 May 2012, 12:54 AM
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
0
Accepted
Dani
Telerik team
answered on 29 May 2012, 09:08 AM
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 >>
0
Murray
Top achievements
Rank 1
answered on 29 May 2012, 09:53 AM
Hi Dani

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

regards

Murray
0
Dani
Telerik team
answered on 29 May 2012, 06:48 PM
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 >>
0
Murray
Top achievements
Rank 1
answered on 30 May 2012, 12:50 AM
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
Tags
ContextMenu
Asked by
Murray
Top achievements
Rank 1
Answers by
Dani
Telerik team
Murray
Top achievements
Rank 1
Share this question
or