我有一个工作的动态菜单,它将数据绑定到由应用程序动态控制的项目的分层集合。对于低于参考是WPF声明:功能区控件的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色带控制呈现此底层菜单结构,它使用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,但有关于应用于RibbonGroup的RibbonTab样式的错误。另一个相关的问题是:如果AppAction对象具有Ribbon控件样式的判别器属性(即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 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);
}
}
你为什么使用'Resources'和'DataTemplate'?你想做什么? – MoHaKa 2013-04-11 14:16:50
我正在使用资源,因为假定HierarchicalDataTemplate不会使用嵌套级别而不使用资源。通过类比工作后,资源是一个剩余的。第二个代码段不起作用(还)。 – too 2013-04-11 14:29:18