views:

201

answers:

1

Hi

I want to create a usercontrol in WPF through which i want to expose a collection property. I want to change the UI of the usercontrol based on the changes in collection.

For example lets say i have a collection of strings which is binded to my usercontrol. Based on that collection i want to create buttons on the usercontrol containing those text as button text. Is there a way i can acieve this.

+1  A: 

Yes, you can set a DataTemplate containing a button for an ItemsControl control that is binded to that collection. For Example:

//For code:
items.DataContext = new List<string>
{
    "Item 1",
    "Item 2",
    "Item 3"
};

//For XAML            
<ItemsControl x:Name="items" ItemsSource="{Binding}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button Content="{Binding}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
idursun
Thanks very much.
deepak