27

I am trying to bind several different properties in my Xaml:

<Label Content="{Binding Description}" Visibility="{Binding Path=DescriptionVisibility, ElementName=_UserInputOutput}" FontSize="{Binding Path=FontSizeValue, ElementName=_UserInputOutput}" HorizontalAlignment="Left" VerticalAlignment="Top" Padding="0" /> 

You will noticed I have used two Different binding techniques here. The ones using Element Name work, the other does not. Here is code behind:

public string Description { get { return (string)GetValue(DescriptionProperty); } set { SetValue(DescriptionProperty, value); } } public static readonly DependencyProperty DescriptionProperty = DependencyProperty.Register("Description", typeof(string), typeof(UserControl), new UIPropertyMetadata("")); 

Each Binding has a different name but they all look like this for the most part. I want my Binding to be able to work with:

{Binding Description} 

Instead of:

{Binding Path=Description, ElementName=_UserInputOutput} 

It only seems to be working when ElementName is used. I need to export/import this XAML, so I can't have a ElementName or the import won't work.

I thought this would be best:

{Binding Path=Description, RelativeSource={RelativeSource Self}} 

This did not work.

Any ideas?? Thank you!

1
  • This article might be of interest. Commented Aug 16, 2012 at 23:01

2 Answers 2

41

{RelativeSource Self} targets the object that owns the property that is being bound, if you have such a binding on a Label it will look for Label.Description, which isn't there. Instead you should use {RelativeSource AncestorType=UserControl}.

Bindings without a source (ElementName, Source, RelativeSource) are relative to the DataContext, however in UserControls you should avoid setting the DataContext to not mess with external bindings.

Sign up to request clarification or add additional context in comments.

Comments

38

You haven't set the DataContext, which is what the RelativeSource is using to determine what it's relative to. You need to set the DataContext at a higher level, like the UserControl. I typically have:

<UserControl ... DataContext="{Binding RelativeSource={RelativeSource Self}}"> </UserControl> 

This tells the UserControl to bind itself the class in the codebehind.

4 Comments

"which is what the RelativeSource is using to determine what it's relative to" That statement is plain incorrect. Also setting the DataContext of a UserControl is rarely a good idea...
I think this is not a good idea since it violates the MVVM paradigm: When we want to use WPF with MVVM we have a View that binds to its ViewModel. So inside the constructor of the code behind of the View we say View() { InitializeComponent(); DataContext = new ViewModel(); } This makes all data binding operations work on the ViewModel so we can separate the view from the ViewModel.
Its unlikely that you want (or even need) to do what is suggested here. The upvotes seem ill-advised.
@codingdave I agree except there's some thinking that you can simplify using your controls in a hierarchy if you either let the parent set the view model or use binding to bind the dependency.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.