有一個包含一個ListBox一個用戶控件。而ListBox使用另一個UserControl作爲DataTemplate。如何進行數據綁定菜單項集合直接進入XAML
<ListBox x:Uid="SectionList" x:Name="SectionList" ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<expander:ExpanderDataTemplate/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
在後面的代碼中,我將數據上下文設置爲ObservableCollection。 ExpanderItem自曝以下的公共屬性... 字符串名稱, 的ObservableCollection的MenuItems, 布爾擴大, 用戶控件控制 我已經實現INotifyPropertyChanged接口爲這些屬性。
而且我ExpanderDataTemplate看起來像這樣...
<Border BorderThickness="0,1">
<Expander IsExpanded="{Binding Path=Expanded}" Content="{Binding Path=Control}">
<Expander.Header>
<StackPanel>
<TextBlock Text="{Binding Path=Name}"/>
<Menu x:Name="ConfigurationMenu" Background="Transparent">
<MenuItem x:Name="DropDownMenuItem" ItemsSource="{Binding Path=MenuItems}">
<MenuItem.Header>
<Image Source="..\..\images\dropdown_arrow.gif" SnapsToDevicePixels="True" Stretch="None"/>
</MenuItem.Header>
</MenuItem>
</Menu>
</StackPanel>
</Expander.Header>
</Expander>
</Border>
在這裏,你可以看到,我有數據範圍內的全部四個屬性擴展,控制,名稱和的MenuItems。所有的屬性都得到了正確的界定並且可以期待MenuItems。 MenuItems是System.Windows.Controls.MenuItem的ObservableCollection。
我要實現期望的行爲,當我點擊DropDownMenuItem,我應該看到的數據有界的MenuItems集合作爲子菜單。
請幫我解決這個問題。謝謝。
在此刻發生了什麼? – 2010-02-08 22:37:37