WPF - 在自定义用户控件上绑定工具提示文本时遇到问题

发布于 2024-10-05 20:43:41 字数 1662 浏览 1 评论 0原文

我正在创建一个简单的用户控件;只是一个图像按钮。

我已经成功地将图像绑定到按钮,因此我决定添加一个工具提示。现在我遇到了麻烦。看来我可以在控件的 XAML 中对工具提示的文本进行硬编码,但当它绑定时,它会返回一个空字符串。

这是我的控件的 XAML:

<Button x:Class="BCOCB.DACMS.Controls.ImageButton"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
        Name="this"
        Style="{StaticResource DisabledButton}">

    <Image Source="{Binding ElementName=this, Path=Source}" />    
    <Button.ToolTip>
        <TextBlock Text="{Binding ElementName=this, Path=ToolTipText}" />
    </Button.ToolTip>
</Button>

这是工具提示文本的依赖属性信息:

public static readonly DependencyProperty ToolTipTextProperty = DependencyProperty.Register("ToolTipText", typeof(string), typeof(ImageButton));
public string ToolTipText
{
  get
  {
    return this.GetValue(ToolTipTextProperty) as string;
  }
  set
  {
    this.SetValue(ToolTipTextProperty, value);
  }
}

最后,我的窗口中的控件声明:

<controls:ImageButton x:Name="btnAdd" Source="/DACMS;component/Resources/plus.png" ToolTipText="Add New Item" Click="btnAdd_Click" />

正如我之前提到的,图像绑定得很好,而且我已经在同样的方式。

有什么想法吗?

谢谢,
桑尼

编辑:我现在可以使用了。我已从绑定中删除了 ElementName,并在实例化后面的代码中设置了 TextBlock 的 DataContext = this。不过,我想知道如何在 XAML 中解决此问题。

I'm in the process of creating a simple user control; just an ImageButton.

I've already successfully bound the Image to the button and so I've decided to add a tooltip. Now I'm having troubles. It seems that I can hard-code the text for the tooltip in the XAML for the control, but when it's bound it's returning an empty string.

Here's the XAML for my control:

<Button x:Class="BCOCB.DACMS.Controls.ImageButton"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
        Name="this"
        Style="{StaticResource DisabledButton}">

    <Image Source="{Binding ElementName=this, Path=Source}" />    
    <Button.ToolTip>
        <TextBlock Text="{Binding ElementName=this, Path=ToolTipText}" />
    </Button.ToolTip>
</Button>

And here's the dependency property info for the tooltip text:

public static readonly DependencyProperty ToolTipTextProperty = DependencyProperty.Register("ToolTipText", typeof(string), typeof(ImageButton));
public string ToolTipText
{
  get
  {
    return this.GetValue(ToolTipTextProperty) as string;
  }
  set
  {
    this.SetValue(ToolTipTextProperty, value);
  }
}

And, finally, the declaration of the control in my Window:

<controls:ImageButton x:Name="btnAdd" Source="/DACMS;component/Resources/plus.png" ToolTipText="Add New Item" Click="btnAdd_Click" />

As I mentioned before, the image binds just fine and I've done it in exactly the same manner.

Any ideas?

Thanks,
Sonny

EDIT: I have it working now. I've removed the ElementName from the binding and set the TextBlock's DataContext = this in the code behind on instanciation. Still, I'd like to know how to fix this in the XAML, instead.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

忆依然 2024-10-12 20:43:41

我现在无法对此进行测试,但您可以尝试:

<Button.ToolTip
      DataContext=”{Binding Path=PlacementTarget.Parent.Parent,
                    RelativeSource={x:Static RelativeSource.Self}}"
>
    <TextBlock Text="{Binding Path=ToolTipText}" />
</Button.ToolTip>

您可能需要对 PlacementTarget 中“Parent”的数量进行一些实验。

希望这有效。我不喜欢给出未经测试的答案,但我这台计算机上没有 VS。 :)

I'm unable to test this right now, but you can try:

<Button.ToolTip
      DataContext=”{Binding Path=PlacementTarget.Parent.Parent,
                    RelativeSource={x:Static RelativeSource.Self}}"
>
    <TextBlock Text="{Binding Path=ToolTipText}" />
</Button.ToolTip>

You may have to experiment a little with the number of "Parent" in PlacementTarget.

Hopefully this works. I don't like giving answers that I haven't tested, but I don't have VS on this computer. :)

人│生佛魔见 2024-10-12 20:43:41

我在绑定到 ContextMenu 时遇到了同样的问题。经过我的研究,我认为这是因为 ToolTip 和 ContextMenu 不存在于页面/窗口/控件的可视树中。因此,DataContext 不是继承的,并且使绑定变得麻烦。

这是我发现对我有用的 Xaml hack。

绑定到菜单项在 WPF 上下文菜单中

I've had this same problem with binding to a ContextMenu. After my research I think that it is because the ToolTip and ContextMenu do not exist within the visual tree of your page/window/control. And therefore the DataContext is not inherited and makes binding troublesome.

Here is a Xaml hack I found that worked for me.

Binding to a MenuItem in a WPF Context Menu

独闯女儿国 2024-10-12 20:43:41

通过 xaml 将数据上下文设置为“this”的方法如下所示:

<Control DataContext={Binding RelativeSource={RelativeSource Self}}>

另一方面,wpf 按钮允许其内容是您想要的任何(单个)内容。如果您想要文本以外的内容(即文本和图像),它看起来像这样:

<Button Name="SampleButton" Click="SampleButton_Click">
    <Grid Width="70" Height="62">
        <Label Content="SampleText"/>
        <Image Margin="3,3,3,3" Source="Graphics/sample.ico"/>
    </Grid>
</Button>

The way to set the data context to "this" through xaml looks like this:

<Control DataContext={Binding RelativeSource={RelativeSource Self}}>

As another point, wpf buttons allow their content to be just about any (single) thing you want. If you want something other than text (ie, text and an image), it looks like this:

<Button Name="SampleButton" Click="SampleButton_Click">
    <Grid Width="70" Height="62">
        <Label Content="SampleText"/>
        <Image Margin="3,3,3,3" Source="Graphics/sample.ico"/>
    </Grid>
</Button>
探春 2024-10-12 20:43:41

由于除了工具提示 TextBlock 上的文本之外,您没有更改任何内容,因此您只需使用内联声明即可为您生成 TextBlock,并且不需要任何黑客技术来解决您遇到的名称范围问题

<Button ... ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=ToolTipText}">...

:可以交替地在图像上设置工具提示并将控件用作 DataContext,这可以解决名称范围问题。 DataContext 将传递到 ToolTip,允许正常绑定:

<Image DataContext="{Binding ElementName=this}" Source="{Binding Source}">
    <Image.ToolTip>
        <TextBlock FontSize="18" Text="{Binding Path=ToolTipText}" />
    </Image.ToolTip>
</Image>

这种方式允许在 TextBlock 或更复杂的视觉效果上进行其他设置。

Since you aren't changing anything but the Text on the tooltip TextBlock you can just use an inline declaration which will generate the TextBlock for you and doesn't require any hacking to get around the name scoping issue you're running into otherwise:

<Button ... ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=ToolTipText}">...

You could alternately set the ToolTip on the Image and use the control as the DataContext, which gets around the name scoping problem. The DataContext will be passed to the ToolTip, allowing normal binding:

<Image DataContext="{Binding ElementName=this}" Source="{Binding Source}">
    <Image.ToolTip>
        <TextBlock FontSize="18" Text="{Binding Path=ToolTipText}" />
    </Image.ToolTip>
</Image>

This way allows additional settings on the TextBlock or more complex visuals.

黯然 2024-10-12 20:43:41

这修复了工具提示绑定和依赖项属性的问题:

<UserControl x:Class="Extended.InputControls.TextBoxUserControl" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:Extended.InputControls"
    x:Name="UserControl"
    DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <TextBox x:Name="textBox">
        <TextBox.ToolTip>
            <ToolTip Content="{Binding Path=CustomToolTip}" Background="Yellow"/>
        </TextBox.ToolTip>
    </TextBox>
</UserControl>

而不是这个(不起作用):

<UserControl x:Class="Extended.InputControls.TextBoxUserControl" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:Extended.InputControls"
    x:Name="UserControl">
    <TextBox x:Name="textBox">
        <TextBox.ToolTip>
            <ToolTip Content="{Binding ElementName=UserControl, Path=CustomToolTip}" Background="Yellow"/>
        </TextBox.ToolTip>
    </TextBox>
</UserControl>

This fixes the Problem with the Tooltip Bindings and Dependencies Properties:

<UserControl x:Class="Extended.InputControls.TextBoxUserControl" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:Extended.InputControls"
    x:Name="UserControl"
    DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <TextBox x:Name="textBox">
        <TextBox.ToolTip>
            <ToolTip Content="{Binding Path=CustomToolTip}" Background="Yellow"/>
        </TextBox.ToolTip>
    </TextBox>
</UserControl>

Instead of this ( doesnt Work ):

<UserControl x:Class="Extended.InputControls.TextBoxUserControl" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:Extended.InputControls"
    x:Name="UserControl">
    <TextBox x:Name="textBox">
        <TextBox.ToolTip>
            <ToolTip Content="{Binding ElementName=UserControl, Path=CustomToolTip}" Background="Yellow"/>
        </TextBox.ToolTip>
    </TextBox>
</UserControl>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文