1

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!

2 Answers 2

0

In the codebehind

public class MyUserControl : UserControl
{
    #region Text
    /// <summary>
    /// The <see cref="DependencyProperty"/> for <see cref="Text"/>.
    /// </summary>
    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register(
            RunningPropertyName,
            typeof(string),
            typeof(MyUserControl ),
            new UIPropertyMetadata(null));

    /// <summary>
    /// The name of the <see cref="Text"/> <see cref="DependencyProperty"/>.
    /// </summary>
    public const string TextPropertyName = "Text";

    /// <summary>
    /// The text to display
    /// </summary>
    public string Text
    {
        get { return (string)GetValue(TextProperty ); }
        set { SetValue(TextProperty , value); }
    }
    #endregion      
}

In the xaml

<UserControl x:Class="Derp.MyUserControl" 
             x:Name="root"
             SnippingXamlForBrevity="true"

and later...

<TextBlock Text="{Binding Text, ElementName=root}" />
3
  • Will, you are a star! I've been looking at this since last week and made little progress. Thanks a lot. Now I'd just like to understand why it works! I guess it's the ElementName=root so I'll have a look through my WPF books now. Jul 11, 2011 at 10:35
  • @ChrisTrollen: The DP is defined on the class. This class' public properties are found at the root of the UserControl's definition in xaml. Therefore, you can access these public properties in bindings by somehow referencing the root node of the UserControl. You can do this by either giving it a name x:Name="something" or by using a relative source {Binding Text, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}.
    – user1228
    Jul 11, 2011 at 11:20
  • Ah, right - thanks. I had tried using RelativeSource but I didn't think to set the AncestorType to the user control itself. Jul 11, 2011 at 12:42
0

InternalText seems to be a not dependency property. Try to convert it into one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.