如何将元素绑定到属于控件的根元素的属性? [英] How to bind an element to a property which belongs to the root element of a control?

查看:125
本文介绍了如何将元素绑定到属于控件的根元素的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


问题可能听起来有点混乱,但我目前面临的问题是这样的:

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

只有第二个绑定可以设置按钮的内容。第一个,我想用来设置按钮的工具提示的内容(通过LabelText依赖属性)不起作用。



是否可以做第一个绑定工作?
谢谢。

解决方案

尝试这样:

 < 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 / p>

The question may sound a little confusing, but the problem I'm currently facing is this:

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

Only the second binding works, which sets the content of the button. The first one, which I would like to use to set the contents of the tooltip of the button (via the LabelText dependency property) does not work.

Is it possible to make the first binding work? Thanks.

解决方案

Try this:

<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>

We add a ToolTip element and assign it's DataContext as it's PlacementTarget which should then reach the TextBlock

这篇关于如何将元素绑定到属于控件的根元素的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆