views:

1949

answers:

3

I've modified my question since it has changed focus when trying things out. I narrowed the problem down to the following...

I try to bind the selected Item of a TreeView to a StackPanel (or some other container that can hold User Controls). This container will then display a UserControl, depending on the type of the selected item.

Here is the xaml of the StackPanel (both treeview and stackpanel are in the same window ==> different grid column)

<StackPanel Grid.Column="2" MinWidth="500" DataContext="{Binding ElementName=myTree, Path=SelectedItem, Mode=OneWay}">
    <StackPanel.Resources>
        <DataTemplate DataType="{x:Type mvTypes:MyTypeA}">
            <controls:UserControlA DataContext="{Binding}" />
        </DataTemplate>
        <DataTemplate DataType="{x:Type mvTypes:MyTypeB}">
            <controls:UserControlB DataContext="{Binding}" />
        </DataTemplate>
    </StackPanel.Resources>
</StackPanel>

When I place a user control directly under the stackpanel (not in the resources), it displays it with the selected object as their datacontext. Idem if I place a TextBox in it, it will show the correct type of the selected item.

<TextBox Name="textBox1" Text="{Binding}" />

For some reason, placing it within a DataTemplate (even without setting the DataType) results in nothing to display.

Any sugestions. I'm thinking that maybe a StackPanel is not the right control for this, though I can't seem to find other controls that look suitable as containers like this.

Thanks in advance.

A: 

Although you have set the Binding on the second custom control, are you setting the DataContext, as the binding is the route to the information and the DataContext is the information it applies this binding information to.

Andrew

REA_ANDREW
Yes, datacontext is set in the custom control like this... (very simplified of course)ModelObject c = repo.GetByID(3295123);ViewModelTree vmt = new ViewModelTree(c);base.DataContext = vmt;Could it be that I'm only binding to the the DataContext of the user control, not window and therefore not visible to other controls in the window?
Littlefool
A: 

You can create a UserControl to display the TreeView and the selection info on the right, all in one. It saves you from creating any custom control. A custom control is basically unnecessary since you do not create anything which didn't exist before.

<UserControl x:Class="NameSpace.SelectionView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:controls="namespace.Controls"
    Height="300" Width="300">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <TreeView Name="customTree">
            <!--Items go here-->
        </TreeView>
        <StackPanel Grid.Column="1" MinWidth="50" DataContext="{Binding ElementName=customTree, Path=SelectedItem, Mode=OneWay}">
            <StackPanel.Resources>
                <DataTemplate DataType="{x:Type StylingTest:CustomViewModelA}">
                    <controls:CustomADetailsControl />
                </DataTemplate>
                <DataTemplate DataType="{x:Type StylingTest:CustomViewModelB}">
                    <controls:CustomBDetailsControl />
                </DataTemplate>
            </StackPanel.Resources>
            <TextBlock Text="{Binding}"/>
        </StackPanel>
    </Grid>
</UserControl>

Any other custom behaviour, I'm sure you could create or set in styles/templates here.

Also, you might find one of my other answers useful.

Good luck with wpf, cheers.

kek444
Thanks. It's what I've been trying to accomplish now, only directly in the window xaml (not a usercontrol). Though, the selection of the detail (user)controls in the stackpanel does not work, the TextBlock does display the type of the selected treeview item.
Littlefool
+3  A: 

Replace the StackPanel in your example with ContentPresenter and instead of DataContext set the Content property. That should work.

Bryan Anderson
Exactly!DataContext doesn't mean anything, it's just for binding. Content property is what's there for presenting things.
Oleg Mihailik
Thank you so much. It works great. Any input on why it didn't work using the StackPanel?
Littlefool
As Oleg said, DataContext doesn't mean anything by itself, it is just where any bindings below that will work. Think of it like setting a stack frame or variable scope to look in. What you needed was to actually create a binding and have it evaluated and displayed, which is what the ContentPresenter (for a single item) or ItemsPresenter (for a collection) are made for. Try looking up ContentPresenters and DataTemplates and see how they actually interact. I don't know of any good blog posts on it but there are probably some out there.
Bryan Anderson