UdoQQ
UdoQQ

Reputation: 55

How to bind to child element property in TreeView

I have tree view in wpf, it looks like:

CategoryName1

CategoryName2

...

How i can use in my CategoryNames image from child[0]? I tried binding on element name, but this is not working.

My xaml code is:

<TreeView.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding Path=Items}">
        <StackPanel Orientation="Horizontal">
            <Image Source="here i dont know how to do properly"/>
            <TextBlock Text="{Binding Path=Name}"/>
        </StackPanel>
        <HierarchicalDataTemplate.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Image x:Name="img"
                           Source="{Binding Path=ImageSource}"/>
                    <TextBlock Text="{Binding Path=Name}"/>
                </StackPanel>
            </DataTemplate>
        </HierarchicalDataTemplate.ItemTemplate>
    </HierarchicalDataTemplate>
</TreeView.ItemTemplate>

Upvotes: 0

Views: 74

Answers (1)

UdoQQ
UdoQQ

Reputation: 55

The solution is simple, but i dont know how correct it is

We can refer like this

<TreeView.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding Path=Items}">
        <StackPanel Orientation="Horizontal">
            <Image Source="{Binding Path=Items[0].ImageSource}"/>
            <TextBlock Text="{Binding Path=Name}"/>
        </StackPanel>
        <HierarchicalDataTemplate.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Image x:Name="img"
                           Source="{Binding Path=ImageSource}"/>
                    <TextBlock Text="{Binding Path=Name}"/>
                </StackPanel>
            </DataTemplate>
        </HierarchicalDataTemplate.ItemTemplate>
    </HierarchicalDataTemplate>
</TreeView.ItemTemplate>

Upvotes: 0

Related Questions