WPF自定义控件的ToolTip MultiBinding问题

发布于 2024-10-24 07:39:29 字数 1271 浏览 2 评论 0原文

当我在 WPF 自定义控件中设置工具提示绑定时,这种方式效果完美:

public override void OnApplyTemplate()
{
    base.OnApplyTemplate();
   ...
    SetBinding(ToolTipProperty, new Binding
                        {
                            Source = this,
                            Path = new PropertyPath("Property1"),
                            StringFormat = "ValueOfProp1: {0}"
                        });          
}

但是当我尝试使用 MultiBinding 在工具提示中包含多个属性时,它不起作用:

public override void OnApplyTemplate()
{
    base.OnApplyTemplate();
   ...
    MultiBinding multiBinding = new MultiBinding();
    multiBinding.StringFormat = "ValueOfProp1: {0}\nValueOfProp2: {1}\nValueOfProp3: {2}\n";

        multiBinding.Bindings.Add(new Binding
        {
            Source = this,
            Path = new PropertyPath("Property1")
        });
        multiBinding.Bindings.Add(new Binding
        {
            Source = this,
            Path = new PropertyPath("Property2")
        });
        multiBinding.Bindings.Add(new Binding
        {
            Source = this,
            Path = new PropertyPath("Property3")
        });

        this.SetBinding(ToolTipProperty, multiBinding);          
}  

在这种情况下,我根本没有显示任何工具提示。

我哪里错了?

When I set a ToolTip Binding In a WPF Custom Control, this way it works perfect:

public override void OnApplyTemplate()
{
    base.OnApplyTemplate();
   ...
    SetBinding(ToolTipProperty, new Binding
                        {
                            Source = this,
                            Path = new PropertyPath("Property1"),
                            StringFormat = "ValueOfProp1: {0}"
                        });          
}

But when I try to use MultiBinding to have several properties in the ToolTip, it doesn't work:

public override void OnApplyTemplate()
{
    base.OnApplyTemplate();
   ...
    MultiBinding multiBinding = new MultiBinding();
    multiBinding.StringFormat = "ValueOfProp1: {0}\nValueOfProp2: {1}\nValueOfProp3: {2}\n";

        multiBinding.Bindings.Add(new Binding
        {
            Source = this,
            Path = new PropertyPath("Property1")
        });
        multiBinding.Bindings.Add(new Binding
        {
            Source = this,
            Path = new PropertyPath("Property2")
        });
        multiBinding.Bindings.Add(new Binding
        {
            Source = this,
            Path = new PropertyPath("Property3")
        });

        this.SetBinding(ToolTipProperty, multiBinding);          
}  

In this case I have no ToolTip shown at all.

Where am I wrong?

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

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

发布评论

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

评论(1

悍妇囚夫 2024-10-31 07:39:29

事实证明,MultiBinding 上的 StringFormat 仅适用于 string 类型的属性,而 ToolTip 属性的类型为对象。在这种情况下,MultiBinding 需要定义一个值转换器。

作为解决方法,您可以将 TextBlock 设置为 ToolTip 并使用 MultiBinding 绑定其 Text 属性(因为 >Text 的类型为 string ,它将与 StringFormat 一起使用):

TextBlock toolTipText = new TextBlock();

MultiBinding multiBinding = new MultiBinding();
multiBinding.StringFormat = "ValueOfProp1: {0}\nValueOfProp2: {1}\nValueOfProp3: {2}\n";

multiBinding.Bindings.Add(new Binding
{
    Source = this,
    Path = new PropertyPath("Property1")
});
multiBinding.Bindings.Add(new Binding
{
    Source = this,
    Path = new PropertyPath("Property2")
});
multiBinding.Bindings.Add(new Binding
{
    Source = this,
    Path = new PropertyPath("Property3")
});

toolTipText.SetBinding(TextBlock.TextProperty, multiBinding);

ToolTip = toolTipText;

It turns out that StringFormat on MultiBinding works only on properties of type string, while the ToolTip property is of type object. In this case the MultiBinding requires a value converter defined.

As a workaround you could set a TextBlock as a ToolTip and bind its Text property using MultiBinding (since Text is of type string it'll work with StringFormat):

TextBlock toolTipText = new TextBlock();

MultiBinding multiBinding = new MultiBinding();
multiBinding.StringFormat = "ValueOfProp1: {0}\nValueOfProp2: {1}\nValueOfProp3: {2}\n";

multiBinding.Bindings.Add(new Binding
{
    Source = this,
    Path = new PropertyPath("Property1")
});
multiBinding.Bindings.Add(new Binding
{
    Source = this,
    Path = new PropertyPath("Property2")
});
multiBinding.Bindings.Add(new Binding
{
    Source = this,
    Path = new PropertyPath("Property3")
});

toolTipText.SetBinding(TextBlock.TextProperty, multiBinding);

ToolTip = toolTipText;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文