简体   繁体   中英

Silverligth, how to give an observablecollection of UserControl as itemsource to a listbox

I have a list of observable collection of UserControl in my MVVM silverlight project. I want to give them as source to a listbox datasource. How can I achieve this.

 <ListBox VerticalAlignment="Stretch" Style="{StaticResource Menu}" ItemsSource="{Binding Menus}" DisplayMemberPath="Libelle" SelectedItem="{Binding SelectedItem, Mode=TwoWay}">

<ItemsControl ItemsSource="{Binding Menus.UnderControl}" />                                </ListBox>

Thanks.

Let us say you have a UserControl called myUserControl1 , and myUserControl1.Menus is an ObservableCollection .

and you wish to bind myListBox1.ItemsSource to myUserControl1.Menus. Then you should use the ElementName key word in your binding. For example:

<my:CustomControl x:Name="myUserControl1" />
<ListBox x:Name="myListBox1" 
         ItemsSource="{Binding Menus,ElementName=myUserControl1}" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM