如何绑定ListViewItem的的工具提示它的ContentTemplate内部TextBlock的工具提示 [英] How to Bind ListViewItem's ToolTip to a ToolTip of TextBlock inside its ContentTemplate

查看:130
本文介绍了如何绑定ListViewItem的的工具提示它的ContentTemplate内部TextBlock的工具提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的ListViewItem的工具提示绑定到它的ContentTemplate的TextBlock中的工具提示。

我试过以下,但它没有工作:

 < ListView控件的ItemsSource ={结合DoestMatter}>
    < ListView.ItemContainerStyle>
        <风格的TargetType ={X:类型的ListViewItem}>
            < setter属性=工具提示VALUE ={绑定的ElementName =标题,路径=工具提示}/>
            < setter属性=的ContentTemplate>
                < Setter.Value>
                    <&DataTemplate的GT;
                        < TextBlock的X:名称=标题文本={结合标题}工具提示=测试/>
                    < / DataTemplate中>
                < /Setter.Value>
            < /二传手>
        < /样式和GT;
    < /ListView.ItemContainerStyle>
< /&的ListView GT;

工具提示的值是动态生成的,在这里我只是显示了它作为静态字符串,但实际上它不是,这就是为什么我需要将其绑定到TextBlock的工具提示。

我怎样才能使它工作?


解决方案

您code工作得很好,因为它是...好,我不得不改变一些绑定 s到得到它的工作,但主要的XAML是罚款:

 < ListView控件的ItemsSource ={结合试验}>
    < ListView.ItemContainerStyle>
        <风格的TargetType ={X:类型的ListViewItem}>
            < setter属性=工具提示VALUE ={结合工具提示的ElementName =标题}/>
            < setter属性=的ContentTemplate>
                < Setter.Value>
                    <&DataTemplate的GT;
                        < TextBlock的X:名称=标题文本={绑定名称}
                            工具提示=测试/>
                    < / DataTemplate中>
                < /Setter.Value>
            < /二传手>
        < /样式和GT;
    < /ListView.ItemContainerStyle>
< /&的ListView GT;

但是,我也注意到有一件事是,它只有在你把你的鼠标直接在的TextBlock 的未拉伸工作跨越的 ListViewItem的的宽度。为了解决这个问题,就在 Horizo​​ntalContentAlignment 属性设置为

 < ListView控件的ItemsSource ={结合试验}>
    < ListView.ItemContainerStyle>
        <风格的TargetType ={X:类型的ListViewItem}>
            < setter属性=工具提示VALUE ={结合工具提示的ElementName =标题}/>
            < setter属性=Horizo​​ntalContentAlignmentVALUE =弹力/>
            < setter属性=的ContentTemplate>
                < Setter.Value>
                    <&DataTemplate的GT;
                        < TextBlock的X:名称=标题文本={绑定名称}
                            工具提示=测试/>
                    < / DataTemplate中>
                < /Setter.Value>
            < /二传手>
        < /样式和GT;
    < /ListView.ItemContainerStyle>
< /&的ListView GT;


更新>>>

您说得对......的工具提示仍然在的TextBlock 。然后你只需要更新您的 ToolTip.Binding 来*,关于文本属性适用相同的值:

 < ListView控件的ItemsSource ={结合试验}>
    < ListView.ItemContainerStyle>
        <风格的TargetType ={X:类型的ListViewItem}>
            < setter属性=工具提示VALUE ={结合工具提示}/>
            < setter属性=的ContentTemplate>
                < Setter.Value>
                    <&DataTemplate的GT;
                        < TextBlock的X:名称=标题文本={结合标题}
                            工具提示={结合工具提示}/>
                    < / DataTemplate中>
                < /Setter.Value>
            < /二传手>
        < /样式和GT;
    < /ListView.ItemContainerStyle>
< /&的ListView GT;

现在你应该看到2提示,一是当过的TextBlock 和一个当过 ListViewItem的


更新2 >>>

@Ron,我真的不明白,为什么你正在服用这种消极的反应,我的回答(S)...你的真正的应该看你的态度,因为我的尝试来帮助你,我不认为我配得上你的态度不好。因此,为了解决你的第二个,甚至粗鲁的评论:


  

      
  1. 我说的Horizo​​ntalAlignment设置默认为Stretch

  2.   

真的吗?你在哪儿说?事实上,你的没有的说,你说的的ListViewItem的延伸上默认的,这是完全不同的东西。正如评论所说,我被拉伸的TextBlock 中的 ListViewItem的 Horizo​​ntalContentAlignment 属性,它的的设置为 Strecth 默认


  

      
  1. 谁说我要的标题作为我的工具提示?

  2.   

没人说,但是你确实说的是动态生成的工具提示的值的...所以我只能像你是绑定动态生成工具提示。的如果的是这样的话,那么你可以简单的数据绑定的相同值 ListViewItem.ToolTip 属性为好。


更新3 >>>

在回答您最后的评论:


  

我坚持我的问​​题从一开始,因为我真的不能说明问题。我知道什么,虽然我不解决认识实际的方法。我想绑定到的的TextBlock 工具提示属性。


好后悔,但你不能因为的TextBlock 是在的DataTemplate 宣布做,在XAML。你只能访问的DataTemplate 产生的code元素,因为他们不只是存在,直到运行时...看到的如何:查找的DataTemplate生成的元素 MSDN上页找出如何做到这一点。所以你必须找到另一种方式来实现自己的目标,这就是为什么我一直一直暗示其他方法。

I would like to bind my ListViewItem's ToolTip to its ContentTemplate's TextBlock's ToolTip.

I tried the following but it didnt work:

<ListView ItemsSource="{Binding DoestMatter}" >
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="ToolTip" Value="{Binding ElementName=Title, Path=ToolTip}"/>
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <TextBlock x:Name="Title" Text="{Binding Title}" ToolTip="Test"/>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

The ToolTip's value is dynamically generated, here I just showed it as static string but actually it isn't, and that's why I need to bind it to the TextBlock's ToolTip.

How can I make it work?

解决方案

Your code works just fine as it is... well, I had to change some Bindings to get it to work, but the main XAML is fine:

<ListView ItemsSource="{Binding Tests}" >
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="ToolTip" Value="{Binding ToolTip, ElementName=Title}" />
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <TextBlock x:Name="Title" Text="{Binding Name}" 
                            ToolTip="Test" />
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

However, one thing that I did notice was that it only works if you put your cursor directly over the TextBlock and that was not stretching across the width of the ListViewItem. To fix that, just set the HorizontalContentAlignment property to Stretch:

<ListView ItemsSource="{Binding Tests}" >
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="ToolTip" Value="{Binding ToolTip, ElementName=Title}" />
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <TextBlock x:Name="Title" Text="{Binding Name}" 
                            ToolTip="Test" />
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>


UPDATE >>>

You're right... the ToolTip was still on the TextBlock. Then you just need to update your ToolTip.Binding to *the same value that works on the Text property:

<ListView ItemsSource="{Binding Tests}" >
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="ToolTip" Value="{Binding ToolTip}" />
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <TextBlock x:Name="Title" Text="{Binding Title}" 
                            ToolTip="{Binding ToolTip}" />
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

Now you should see 2 tooltips, one when over the TextBlock and one when over the ListViewItem.


UPDATE 2 >>>

@Ron, I really cannot understand why you are taking such a negative reaction to my answer(s)... you really should watch your attitude, because I am trying to help you and I don't feel that I deserve your bad attitude. So, to address your second, even ruder comment:

  1. I said that the HorizontalAlignment is set to Stretch by default

Really? Where did you say that? In fact, you didn't say that, you said The ListViewItem stretches on default, which is something entirely different. As mentioned in the comment, I was stretching the TextBlock inside the ListViewItem with the HorizontalContentAlignment property, which isn't set to Strecth by default.

  1. Who said that I want the Title as my ToolTip?

Nobody said that, but you did say The ToolTip's value is dynamically generated... so I can only image that you are Binding your dynamically generated ToolTip. If this is so, then you can simply data Bind that same value to the ListViewItem.ToolTip property as well.


UPDATE 3 >>>

In response to your last comment:

I stick to my question from the beginning because I cant really explain the problem. I know whats the solution though I dont know the practical way. I want to bind to the TextBlock's ToolTip Property.

Well sorry, but you can't do that in XAML because the TextBlock is declared in a DataTemplate. You can only access DataTemplate generated elements in code because they don't just exist until runtime... see the How to: Find DataTemplate-Generated Elements page on MSDN to find out how to do that. So you'll have to find another way to achieve your goal and that's why I've been suggesting these other methods all along.

这篇关于如何绑定ListViewItem的的工具提示它的ContentTemplate内部TextBlock的工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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