6

アプリケーションによって動的に制御されるアイテムの階層コレクションにデータバインドされた動的メニューが機能しています。以下の参照用に、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 リボン コントロールを使用して、この基礎となるメニュー構造をレンダリングしたいと考えています。最初は、レベル 0 の場合は RibbonTab と RibbonGroup、レベル 1 の場合は 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);
    }
}
4

1 に答える 1

9

遅れて申し訳ありません。問題の解決策を探していましたが、残念ながら見つかりませんでした。 HierarchicalDataTemplateリボン レベルが同じタイプではないため、役に立ちません。

あなたの問題を解決する唯一の方法は、DataTemplateレベルごとに新しいものを作ることだと思います。これが良い解決策ではないことはわかっていますが、これが唯一の方法だと思います。より良い方法を見つけたら、知識を共有して教えてください。

注: Type が 1 つあるため、Menu状況によっては使用できます。HierarchicalDataTemplateMenuItem

編集:

これは私が到達したものです:

MainWindow.xaml.cs:

public ObservableCollection<AppAction> child
{
    get
    {
        ObservableCollection<AppAction> reVal = new ObservableCollection<AppAction>();
        reVal.Add(
            new AppAction() { Header = "File", Items = new ObservableCollection<AppAction>() { 
                new AppAction() { Header = "Font", Items = new ObservableCollection<AppAction>() { 
                    new AppAction() { Header = "Arial" }, 
                    new AppAction() { Header = "Segoe UI" }, 
                    new AppAction() { Header = "Tahoma" } } }, 
                new AppAction() { Header = "Other", Items = new ObservableCollection<AppAction>() { 
                    new AppAction() { Header = "Colse" } } } } });

        reVal.Add(
            new AppAction() { Header = "View", Items = new ObservableCollection<AppAction>() { 
                new AppAction() { Header = "A", Items = new ObservableCollection<AppAction>() { 
                    new AppAction() { Header = "AButton" } } }, 
                new AppAction() { Header = "B", Items = new ObservableCollection<AppAction>() { 
                    new AppAction() { Header = "BButton" } } }, 
                new AppAction() { Header = "C", Items = new ObservableCollection<AppAction>() { 
                    new AppAction() { Header = "CButton" } } } } });
        return reVal;
    }
}

そして MainWindow.xaml で:

<Window x:Class="rebbon.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Name="wind">

    <Window.Resources>
        <DataTemplate x:Key="buttonTempl">
            <RibbonButton Label="{Binding Header}"/>
        </DataTemplate>
        <Style TargetType="RibbonGroup" x:Key="groupStyle">
            <Setter Property="Header" Value="{Binding Header}"/>
            <Setter Property="ItemsSource" Value="{Binding Items}"/>
            <Setter Property="ItemTemplate" Value="{StaticResource buttonTempl}"/>
        </Style>        
        <Style TargetType="RibbonTab" x:Key="tabStyle">
            <Setter Property="Header" Value="{Binding Header}"/>
            <Setter Property="ItemsSource" Value="{Binding Items}"/>
            <Setter Property="ItemContainerStyle" Value="{StaticResource groupStyle}"/>
        </Style>
    </Window.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <Ribbon ItemContainerStyle="{StaticResource tabStyle}" ItemsSource="{Binding ElementName=wind, Path=child}"/>
    </Grid>
</Window>

結果:

ここに画像の説明を入力

これがお役に立てば幸いです。

私は最善を尽くしました。

于 2013-04-11T14:48:42.270 に答える