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!
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:
This tells the UserControl to bind itself the class in the codebehind.