如何从XAML中为样式指定控件的工具提示? [英] How to specify a ToolTip for a control in a Style from XAML?

查看:137
本文介绍了如何从XAML中为样式指定控件的工具提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Microsoft CodePlex项目的WPF数据网格。我有一个自定义控件,我想要数据绑定到datagrid行的一个字段。我无法为我的生活找出如何在datagrid行上指定一个工具提示。



我最接近的是使用一个带有Setter的RowStyle来设置工具提示,但这只适用于文本。当我尝试将ControlTempalte作为ToolTip的值,它显示在ControlTemplate类型上调用ToString的结果。



我想我需要设置ToolTip的Template属性,但是我似乎无法弄清楚如何做到这一点...

 < dg:DataGrid Name =dgResultsAutoGenerateColumns =True> 

< dg:DataGrid.RowStyle>


< Style TargetType ={x:Type dg:DataGridRow}>

< Setter Property =ToolTip>
< Setter.Value>

< ControlTemplate TargetType ={x:Type ToolTip}>
< StackPanel>
< TextBlock> txt1< / TextBlock>< TextBlock> txt2< / TextBlock>
< / StackPanel>
< / ControlTemplate>


< /Setter.Value>
< / Setter>
< / Style>

< / dg:DataGrid.RowStyle>

< / dg:DataGrid>


解决方案

...



由于某种原因,我无法直接使用Value.Setter设置值。如果我将工具提示的内容定义为静态资源,然后将其设置在DataGrid.RowStyle的Style属性中。



所以,datagrid行样式如下所示:

 < Style TargetType ={x:Type dg:DataGridRow}> 

< Setter Property =ToolTipValue ={StaticResource resKWIC}>
< / Setter>
< / Style>

< / dg:DataGrid.RowStyle>

资源是

 < Window.Resources> 
< StackPanel x:Key =resKWIC>
< TextBlock> f1< / TextBlock>
< TextBlock> f2>< / TextBlock>
< / StackPanel>
< /Window.Resources>

谢谢!


I'm using a the WPF datagrid from the Microsoft CodePlex project. I have a custom control that I want to databind to a field from the row of the datagrid. I can't for the life of me figure out how to specify a tooltip on a datagrid row.

The closest I've come is to use a RowStyle with a Setter to set the tooltip, but this only seems to work for text. When I try to put a ControlTempalte in as the Value for the ToolTip, it displays the result of calling ToString on the ControlTemplate type.

I think I need to set the "Template" property of the ToolTip, but I can't seem to figure out how to do that...

  <dg:DataGrid Name="dgResults" AutoGenerateColumns="True">

            <dg:DataGrid.RowStyle >


            <Style TargetType="{x:Type dg:DataGridRow}">

                <Setter Property="ToolTip"  >
                    <Setter.Value>

                        <ControlTemplate TargetType="{x:Type ToolTip}">
                           <StackPanel>
                                 <TextBlock>txt1</TextBlock><TextBlock>txt2</TextBlock>
                           </StackPanel>
                        </ControlTemplate>


                    </Setter.Value>
                </Setter>
            </Style>

        </dg:DataGrid.RowStyle>

  </dg:DataGrid>

解决方案

Figured it out... took me about 6 hours...

For some reason, I can't set the value directly using Value.Setter. If I define the content for the tooltip as a static resource though, and then set it in the Style property of the DataGrid.RowStyle it works.

So, the datagrid row style looks like:

            <Style TargetType="{x:Type dg:DataGridRow}">

                <Setter Property="ToolTip" Value="{StaticResource resKWIC}">
                </Setter>                 
            </Style>

        </dg:DataGrid.RowStyle>

And the resource is

<Window.Resources>
    <StackPanel x:Key="resKWIC">
        <TextBlock>f1</TextBlock>
        <TextBlock>f2></TextBlock>
    </StackPanel>
</Window.Resources>

Thanks!

这篇关于如何从XAML中为样式指定控件的工具提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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