RadContextMenu图标在动态绑定中不可见

时间:2012-08-27 13:11:18

标签: wpf data-binding telerik icons contextmenu

我在使用Icon显示上下文菜单RadContextMenu时遇到问题,我正在使用动态绑定到可观察集合,其中包含上下文菜单项(文本和图标),我正在遵循本教程“{{3} }“,以下是我的XAML

<!-- Context Menu Resources Start-->
    <Style x:Key="MenuItemStyle" TargetType="{x:Type telerik:RadMenuItem}">
            <Setter Property="Icon">
                <Setter.Value>
                    <Image Source="{Binding IconPath, Converter={StaticResource pathtoimage}}" />
                </Setter.Value>
            </Setter>
        </Style>

        <HierarchicalDataTemplate x:Key="MenuItemTemplate">
            <telerik:RadButton Content="{Binding Title}" HorizontalContentAlignment="Left" Background="Transparent" BorderBrush="Transparent" />
        </HierarchicalDataTemplate>

<!-- Context Menu Resources End -->

以下是RadGridView的XAML,其中放置了上下文菜单。

<telerik:RadGridView x:Name="myGridView" AutoGenerateColumns="False" ItemsSource="{Binding GridData}" VerticalAlignment="Top">
        <telerik:RadGridView.Columns>
            <telerik:GridViewDataColumn DataMemberBinding="{Binding Result}" Header="Result"/>
            <telerik:GridViewDataColumn DataMemberBinding="{Binding Category}" Header="Category"/>
        </telerik:RadGridView.Columns>
        <telerik:RadContextMenu.ContextMenu>
            <telerik:RadContextMenu ItemsSource="{Binding ContextMenuItems}" ItemContainerStyle="{StaticResource MenuItemStyle}" ItemTemplate="{StaticResource MenuItemTemplate}">                 
            </telerik:RadContextMenu>

查看模型如下

class MainViewModel
{
    private ObservableCollection<MenuItemObj> menuitems_;
    public ObservableCollection<MenuItemObj> ContextMenuItems
    {
        get
        {
            return menuitems_;
        }
        set
        {
            menuitems_ = value;
        }
    }

    private ObservableCollection<GridRowDataObj> griddata_;
    public ObservableCollection<GridRowDataObj> GridData
    {
        get
        {
            return griddata_;
        }
        set
        {
            griddata_ = value;
        }
    }
}

public class MenuItemObj
{
    public string Title { get; set; }
    public string IconPath { get; set; }
}

public class GridRowDataObj
{
    public string Result { set; get; }
    public string Category { set; get; }
}

并将DataContext设置为

//TODO
        ObservableCollection<GridRowDataObj> gd = new ObservableCollection<GridRowDataObj>();
        gd.Add(new GridRowDataObj() { Result = "Primary School", Category = "SSC" });
        gd.Add(new GridRowDataObj() { Result = "Higher School", Category = "HSSC" });
        gd.Add(new GridRowDataObj() { Result = "Higher School", Category = "HSSC" });

        //TODO
        ObservableCollection<MenuItemObj> mi = new ObservableCollection<MenuItemObj>();
        mi.Add(new MenuItemObj() { Title = "My Item 1", IconPath = "/Images/item1.png" });
        mi.Add(new MenuItemObj() { Title = "My Item 2", IconPath = "/Images/item2.png" });
        mi.Add(new MenuItemObj() { Title = "My Item 3", IconPath = "/Images/item3.png" });

        MainViewModel mvm = new MainViewModel();
        mvm.ContextMenuItems = mi;
        mvm.GridData = gd;

        this.DataContext = mvm;

但是当我运行代码时,这就是我得到的

Output of code

我不确定为什么没有显示第一个和第二个菜单项的图标,我已经检查过仅显示最后一个菜单项的图标。我有完整的项目复制这个场景,如果有人需要它,他/她可以要求?,请帮助我?

2 个答案:

答案 0 :(得分:1)

我在Telerik支持论坛上发布了这个问题,并在这里得到了一个有效的解决方案,

RadContextMenu icon issue

答案 1 :(得分:0)

在您的图片资源中添加x:Shared =“False”,以便为每个图标创建新图片。

<Setter.Value>
    <Image Source="{Binding IconPath, Converter={StaticResource pathtoimage}}" x:Shared="False" />
</Setter.Value>