How can I set a Binding Source to point to "this" UserControl CodeBehind? Eg. From a UserControl MarkdownEditor.xaml
, I want to point to properties within MarkdownEditor.xaml.cs
. Doing this without setting DataContext = this
This is what I did tho I marked @Alex B's solution as answer. I didn't want to set the DataContext
of the entire control to Self, as I am binding to other objects too
{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MarkdownEditor}}, Path=Options.FontFamily}
Try using the following binding:
<MarkdownEditor DataContext="{Binding RelativeSource={RelativeSource Self}}" />
Instead of using your proposed extensive expression:
{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MarkdownEditor}}, Path=Options.FontFamily}
Isn't it easier just setting x:Name
of the UserControl
?
<UserControl
...
x:Name="Control">
<TextBlock Text="{Binding ElementName=Control, Path=SomeText}" />
</UserControl>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With