Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 如何将绑定源设置为";“自我”/用户控制代码隐藏_Wpf_Data Binding - Fatal编程技术网

Wpf 如何将绑定源设置为";“自我”/用户控制代码隐藏

Wpf 如何将绑定源设置为";“自我”/用户控制代码隐藏,wpf,data-binding,Wpf,Data Binding,如何将绑定源设置为指向“this”UserControl CodeBehind?例如,从UserControlMarkdownEditor.xaml,我想指向MarkdownEditor.xaml.cs中的属性。无需设置DataContext=this 更新:我的解决方案 这就是我所做的,我标记了@Alex B的解决方案作为答案。我不想将整个控件的DataContext设置为Self,因为我也绑定到其他对象 {Binding RelativeSource={RelativeSource Ance

如何将绑定源设置为指向“this”UserControl CodeBehind?例如,从UserControl
MarkdownEditor.xaml
,我想指向
MarkdownEditor.xaml.cs
中的属性。无需设置
DataContext=this

更新:我的解决方案 这就是我所做的,我标记了@Alex B的解决方案作为答案。我不想将整个控件的
DataContext
设置为Self,因为我也绑定到其他对象

{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MarkdownEditor}}, Path=Options.FontFamily}

尝试使用以下绑定:

<MarkdownEditor DataContext="{Binding RelativeSource={RelativeSource Self}}" />

不要使用建议的扩展表达式:

{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MarkdownEditor}}, Path=Options.FontFamily}
仅仅设置
UserControl
x:Name
不是很容易吗

<UserControl
        ...
    x:Name="Control">

    <TextBlock Text="{Binding ElementName=Control, Path=SomeText}" />

</UserControl>


这似乎并不总是有效。它在大多数情况下都工作得很好……但有时它找不到ElementName=Control。事实上,替代解决方案(查找祖先相对源)在同一场景中也会失败,因此这是一个麻烦。我有一个框架元素被设置为用户控件中子元素的属性,但它不是可视化树的一部分,因此找不到根用户控件。Doh。