3

これを機能させるのに苦労していて、使用する必要のあるすべてのテンプレートとどうしようもなく混乱しています。これが状況です。

メニューを動的に作成したい。このコードは、オブジェクトのリストを取得し、リストをグループ化してから、メニューのitemsourceを設定します。

navBarControl.NavBarMain.ItemsSource = newActions.GroupBy(Function(p) p.GroupName)

XAMLでのテンプレート作成とデータバインディングについてサポートが必要です。私が探しているのは、一番上のアイテムがグループキーで、次に各キーの子がアイテム自体であるメニューを作成することです。

次に、メニュー項目のクリックでコードを実行できるように、すべての子にクリックハンドラーを設定する必要があります。

これは私が達成するのが難しいことを証明しています。誰かがこれがどのように機能するかについてのXAMLの例を提供できますか?

4

1 に答える 1

2

いくつかの実験と少しの運の後で、私はついに解決策を見つけました。これがこの問題を抱えている他の人の助けになることを願っています。要約すると、子(おそらく孫)を持つデータソース(グループ化)にバインドし、メニューを動的に構築したいと思いました。課題は、すべてのメニュー項目のクリックイベントを単一のイベントハンドラーにルーティングすることでした。これが私が思いついたものです。

<!-- Common handler for ALL menu items. This was a tough one to figure out since I kept thinking this had to be done the template somehow -->
    <Menu MenuItem.Click="navView_Click" >
        <Menu.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding}">
                <!-- Content presenter for the list of IGrouping objects. Binding is done to the Key property on the IGrouping class -->
                <ContentPresenter Content="{Binding Path=Key}"></ContentPresenter>
                <HierarchicalDataTemplate.ItemTemplate>
                    <DataTemplate>
                        <!-- Content presenter for the list of objects in each grouping. Binding is done to the Name property on the custom class -->
                        <ContentPresenter Content="{Binding Path=Name}"></ContentPresenter>
                    </DataTemplate>
                </HierarchicalDataTemplate.ItemTemplate>
            </HierarchicalDataTemplate>
        </Menu.ItemTemplate>
    </Menu> 

そして、これがコードで設定されているitemsourceです。それぞれC#とVB

navBarControl.NavBarMain.ItemsSource = newActions.GroupBy(Function(p) p.GroupName)

navBarControl.NavBarMain.ItemsSource = newActions.GroupBy( p => p.GroupName);
于 2012-03-22T16:23:58.880 に答える