C# 如何将元素绑定到属于控件根元素的属性?

C# 如何将元素绑定到属于控件根元素的属性?,c#,wpf,xaml,data-binding,datacontext,C#,Wpf,Xaml,Data Binding,Datacontext,这个问题听起来可能有点困惑,但我目前面临的问题是: 只有第二个绑定起作用,它设置按钮的内容。第一个,我想通过LabelText dependency属性来设置按钮工具提示的内容,但它不起作用 第一次装订可以吗? 谢谢。试试这个: <Button x:Class="sandbox.BtnLabel" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http:/

这个问题听起来可能有点困惑,但我目前面临的问题是:

只有第二个绑定起作用,它设置按钮的内容。第一个,我想通过LabelText dependency属性来设置按钮工具提示的内容,但它不起作用

第一次装订可以吗? 谢谢。

试试这个:

<Button x:Class="sandbox.BtnLabel"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        x:Name="this">
  <Button.ToolTip>
    <ToolTip DataContext="{Binding Path=PlacementTarget, RelativeSource={RelativeSource Self}}">
      <TextBlock Background="Yellow"
                 Text="{Binding LabelText}" />
    </ToolTip>
  </Button.ToolTip>
  <TextBlock Background="Yellow"
             Text="{Binding ElementName=this,
                            Path=LabelText}" />
</Button>
我们添加一个ToolTip元素,并将其DataContext指定为PlacementTarget,然后该元素将到达TextBlock

<Button x:Class="sandbox.BtnLabel"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        x:Name="this">
  <Button.ToolTip>
    <ToolTip DataContext="{Binding Path=PlacementTarget, RelativeSource={RelativeSource Self}}">
      <TextBlock Background="Yellow"
                 Text="{Binding LabelText}" />
    </ToolTip>
  </Button.ToolTip>
  <TextBlock Background="Yellow"
             Text="{Binding ElementName=this,
                            Path=LabelText}" />
</Button>

我们添加了一个ToolTip元素,并将其DataContext指定为PlacementTarget,然后该元素将到达TextBlock

查看。感谢您的参考,但它不起作用。我得到的错误是:System.Windows.Data错误:4:找不到引用为“ElementName=this”的绑定源。BindingExpression:Path=LabelText;DataItem=null;目标元素为'TextBlock'Name=;目标属性为“文本”类型“字符串”,请尝试使用此。。。。Text={Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Button},Path=LabelText}也不起作用:System.Windows.Data错误:4:找不到引用为'RelativeSource FindAncestor,AncestorType='System.Windows.Controls.Button',AncestorLevel='1'的绑定源。BindingExpression:Path=LabelText;DataItem=null;目标元素为'TextBlock'Name=;目标属性是'Text'类型'String',我尝试将AncestorType设置为'BtnLabel',但出现了相同的错误。请查看。感谢您的参考,但它不起作用。我得到的错误是:System.Windows.Data错误:4:找不到引用为“ElementName=this”的绑定源。BindingExpression:Path=LabelText;DataItem=null;目标元素为'TextBlock'Name=;目标属性为“文本”类型“字符串”,请尝试使用此。。。。Text={Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Button},Path=LabelText}也不起作用:System.Windows.Data错误:4:找不到引用为'RelativeSource FindAncestor,AncestorType='System.Windows.Controls.Button',AncestorLevel='1'的绑定源。BindingExpression:Path=LabelText;DataItem=null;目标元素为'TextBlock'Name=;目标属性是'Text'类型'String',我尝试将AncestorType设置为'BtnLabel',但出现了相同的错误。太棒了!就像我期待的那样!我不知道“PlacementTarget”是如何工作的。谢谢!不幸的是,我不能投票,因为我没有足够的声誉太好了!就像我期待的那样!我不知道“PlacementTarget”是如何工作的。谢谢!不幸的是,我不能投票,因为我没有足够的声誉