我在显示RadContextMenu
带有图标的上下文菜单时遇到问题,我正在使用动态绑定到包含上下文菜单项(文本和图标)的可观察集合,我正在关注本教程“动态绑定”,以下是我的 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;
但是当我运行代码时,这就是我得到的
我不确定为什么不显示第一个和第二个菜单项的图标,我检查了该图标是否仅显示最后一个菜单项。我有完整的项目复制这个场景,如果有人需要他/她可以要求吗?请帮帮我吗?