2012-03-21 6 views
3

私はこれをうまく動作させるのに苦労しています。私は絶えず使用する必要のあるテンプレートすべてと混乱しています。ここに状況があります。WPFのIGroupingにどのようにデータバインドしますか?

メニューを動的に作成したいと考えています。コードはオブジェクトリストグループを受け取り、メニューのitemsourceを設定します。

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

私は、XAMLでのテンプレートとデータバインディングを支援する必要があります。私は、トップ・アイテムがグループ・キーであるメニューを作成し、各キーの子供がアイテムそのものを作成するようにしています。

そして私は私がメニュー項目をクリックでコードを実行できるように、すべての子供のためのクリックハンドラのセットを持っている必要があります。

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

答えて

2

私は最終的に解決策を見つけました。いくつかの実験と少し運の後、私はこれがこの問題の誰かに役立つことを願っています。要約すると、私は子供を持っている(グループ化)データソース、(おそらく孫)に結合し、そしてメニューが動的に構築されていたかったです。課題は、メニュー項目は、単一のイベントハンドラにイベントをクリックしALLをルーティングました。ここに私が思いついたことがあります。

<!-- 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); 
関連する問題