我有一个工作动态菜单,它是数据绑定到由应用程序动态控制的分层项目集合。以下是 WPF 声明供参考:
<Menu Grid.Row="1" Grid.ColumnSpan="2" ItemsSource="{Binding Actions}" Style="{StaticResource ResourceKey=dynamicMenu}">
<Menu.Resources>
<HierarchicalDataTemplate DataType="{x:Type wm:AppAction}" ItemsSource="{Binding Path=Items}">
<HierarchicalDataTemplate.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter Property="IsCheckable" Value="{Binding IsCheckable}" />
<Setter Property="IsChecked" Value="{Binding IsChecked}" />
<Setter Property="Visibility" Value="{Binding Path=IsVisible, Converter={wc:BoolToCollapsedConverter}}"/>
<Setter Property="Command" Value="{Binding Command}" />
<Setter Property="Icon" Value="{Binding Image}" />
<Style.Triggers>
<DataTrigger Binding="{Binding Text}" Value="">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type MenuItem}">
<Separator HorizontalAlignment="Stretch" IsEnabled="False"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</HierarchicalDataTemplate.ItemContainerStyle>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding ImageSource}" />
<TextBlock Text="{Binding Text}" />
</StackPanel>
</HierarchicalDataTemplate>
</Menu.Resources>
</Menu>
现在我想使用 Microsoft Ribbon 控件呈现这个底层菜单结构,最初使用 RibbonTab 和 RibbonGroup 用于级别 0 和 RibbonButton 用于级别 1(每个选项卡上的单个组)。
不幸的是,由于某种原因,它没有显示任何内容。这是我到目前为止所做的声明:
<r:Ribbon Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" x:Name="ribbon" Title="WPF Prototype App" ItemsSource="{Binding Actions}">
<r:Ribbon.Resources>
<DataTemplate DataType="{x:Type wm:AppAction}">
<r:RibbonTab Header="{Binding Text}">
<r:RibbonGroup Header="{Binding Text}" ItemsSource="{Binding Actions}" Width="333">
<r:RibbonGroup.Resources>
<DataTemplate DataType="{x:Type wm:AppAction}">
<r:RibbonButton Label="{Binding Text}" LargeImageSource="{Binding ImageSource}"/>
</DataTemplate>
</r:RibbonGroup.Resources>
</r:RibbonGroup>
</r:RibbonTab>
</DataTemplate>
</r:Ribbon.Resources>
</r:Ribbon>
我有一种感觉,这可能很容易解决。我尝试使用 HierarchicalDataTemplate 但将 RibbonTab 的样式应用于 RibbonGroup 时出现错误。
另一个相关的问题是:如果 AppAction 对象具有功能区控件样式鉴别器属性(即 ControlStyle [Tab, Group, Button, CheckBox, ComboBox]),根据该属性的值动态创建适当的控件有多容易,或者它甚至可能吗?或者对于复杂的场景,只在负责这些任务的后续视图中定义功能区的一部分并在视图可见时附加它们是否更好?
编辑:以下是 AppAction 类的简化版本的内容:
[ContentProperty("Items")]
public class AppAction: PropertyChangedBase
{
public AppActionCollection Items { get; set; }
ICommand command;
public ICommand Command
{
get { return command; }
set { CheckSet(ref command, value); }
}
string text;
public string Text
{
get { return text; }
set { CheckSet(ref text, value); }
}
Uri imageSource;
public Uri ImageSource
{
get { return imageSource; }
set { image = null; CheckSet(ref imageSource, value); NotifyOfPropertyChange(() => Image); }
}
public AppAction()
{
Items = new AppActionCollection();
}
}
AppActionCollection 简化:
public class AppActionCollection: ObservableCollection<AppAction>
{
}
可以通过以下方式创建示例菜单树:
public class TestMenu
{
AppActionCollection menu = new AppActionCollection();
public TestMenu()
{
var m = new AppAction { Text = "File" };
m.Items.Add(new AppAction { Text = "Open" });
m.Items.Add(new AppAction { Text = "Save" });
m.Items.Add(new AppAction { Text = "" });
m.Items.Add(new AppAction { Text = "Exit", Command = ApplicationCommands.Close });
menu.Add(m);
m = new AppAction { Text = "Edit" };
m.Items.Add(new AppAction { Text = "Copy" });
m.Items.Add(new AppAction { Text = "Paste" });
m.Items.Add(new AppAction { Text = "Cut" });
m.Items.Add(new AppAction { Text = "Smile", Command = ApplicationCommands.Close });
menu.Add(m);
}
}