I searched the archives for help but I can’t find anything quite specific enough for my particular issue.
I have a TreeView using MVVM to bind data and all seems good. I want to extend the functionality such that I think using a user control for the TreeView items would be good.
Here is the XAML code for the hierarchical data template used by the TreeViewItems:
<HierarchicalDataTemplate
DataType="{x:Type vm:SiteViewModel}"
ItemsSource="{Binding Children}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding SiteName}"/>
</StackPanel>
</HierarchicalDataTemplate>
I want to replace the TextBlock with my user control:
<uc:MyTextBlock InternalText="{Binding SiteName}"/>
The user control (for now) just contains another TextBlock and has a dependency property called InternalText, i.e.
<TextBlock Text="{Binding Path=InternalText}" />
and I set the DataContext in the constructor of the user control to itself:
public MyTextBlock ()
{
InitializeComponent();
DataContext = this;
}
This isn’t working, but if I just change the template so that it specifies static text it seems to work fine:
<uc:MyTextBlock InternalText="Some site name"/>
So how do I get the bound data to get passed to the user control? It’s probably something simple but I’m new to WPF so I’ve not worked it out yet.
Thanks!
In the codebehind
In the xaml
and later…