tags:

views:

540

answers:

1

I have a UserControl that contains a TreeView. I want the user to be able to set the properties of the inner TreeView control via XAML and I'm not sure how to do that.

I've tried creating a public property on the UserControl to the TreeView, but that only allows me to set a SelectedItemChanged trigger.

I'd like to do something like:

<ExampleUserControl>
    <ExampleUserControl.TreeView.ItemTemplate>
        ...
    </ExampleUserControl.TreeView.ItemTemplate>
</ExampleUserControl>

Or:

<ExampleUserControl TreeView.ItemsSource="{Binding Foo}" />

I would prefer not to create properties in the UserControl for each TreeView property, and I don't want to force the user to define the control in C#.

A: 

As for passing multiple properties to the child control in your user control, you can always expose a Style property.

ie ChildStyle

For the ItemsSource unless you use [Josh Smith's Element Spy / Data Context Spy / Freezable][1] trick, you will have a disconnect on DataContexts.

So either you employ those tricks or simply have 2 properties.

1) the ItemsSource 2) the ChildStyle

The xaml ends up...

    <ChildTreeAnswer:MyControl ItemsSource="{Binding Items}">
        <ChildTreeAnswer:MyControl.ChildStyle>
            <Style>
                <Setter Property="ItemsControl.ItemTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <Border BorderBrush="Black"
                                    BorderThickness="1"
                                    Margin="5">
                                <TextBlock Text="{Binding }" />
                            </Border>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ChildTreeAnswer:MyControl.ChildStyle>                           
    </ChildTreeAnswer:MyControl>

Then in your user control do... (I used a listbox for simplicity sake)

    <ListBox ItemsSource="{Binding ItemsSource}"
             Style="{Binding ChildStyle}" />
JB