【问题标题】:Bind the error data of datagridrow to a tooltip textblock将datagridrow的错误数据绑定到一个tooltip文本块
【发布时间】:2019-12-13 09:58:11
【问题描述】:

我试图在工具提示中显示数据网格行的验证错误消息。这是当我直接使用 Grid 控件的 ToolTip 属性时的代码(没有样式):

<Grid Margin="0,-2,0,-2" ToolTip="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}, Path=(Validation.Errors)[0].ErrorContent}">
                                <Ellipse StrokeThickness="0" Fill="Red" 
                                         Width="{TemplateBinding FontSize}" 
                                         Height="{TemplateBinding FontSize}" />
                                <TextBlock Text="!" FontSize="{TemplateBinding FontSize}" 
                                           FontWeight="Bold" Foreground="White" 
                                           HorizontalAlignment="Center"  />                            
                            </Grid>

现在这会正确显示工具提示。一切都很好。

问题:

一旦我开始单独设置工具提示的样式,绑定就会以某种方式停止工作。这是我正在尝试但不起作用的代码:

<Grid Margin="0,-2,0,-2">
    <Ellipse x:Name="ErrorEllipse" StrokeThickness="0" Fill="Red" 
           Width="{TemplateBinding FontSize}" 
           Height="{TemplateBinding FontSize}" />
           <TextBlock Text="!" FontSize="{TemplateBinding FontSize}" 
               FontWeight="Bold" Foreground="White" 
               HorizontalAlignment="Center"/>
               <Grid.ToolTip>
                   <ToolTip Background="Transparent" BorderBrush="Transparent" Height="Auto" Width="Auto">
                       <Border >
                           <TextBlock Text= "{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}, Path=(Validation.Errors)[0].ErrorContent}"
                                                       MinHeight="20"
                                                       MinWidth="100"
                                                       Foreground="White"
                                                       Background="Red"/>
                        </Border>
                    </ToolTip>
                </Grid.ToolTip>
             </Grid>

我在这里缺少什么以及如何实现正确的绑定?可能是一些基本的东西,但我不知道......

【问题讨论】:

  • 不是样式是你的问题。工具提示不在可视树中。这是一个弹出窗口。因此,您的 relativesource 将找不到 datagridrow。您需要一个位于可视化树中的资源并获取该资源或放置目标。这是对您的问题weblogs.asp.net/monikadyrda/wpf-tooltip-content-binding 的一个超级简单的解释,顺便说一句,您应该用谷歌搜索。还。 [0] 改用 / 不会出现错误。

标签: wpf data-binding datagrid


【解决方案1】:

您应该使用 ControlTemplate 单独设置 ToolTip 的样式。

<Style x:Key="ValidationToolTipStyle" TargetType="{x:Type ToolTip}">
    <Setter Property="Background" Value="Transparent" />
    ... 
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ToolTip}">
                <Border >
                    <TextBlock 
                        Text="{TemplateBinding Content}"
                        MinHeight="20"
                        MinWidth="100"
                        Foreground="White"
                        Background="Red"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

然后在您的 XAML 中,像这样使用工具提示:

<Grid Margin="0,-2,0,-2">
    <Ellipse x:Name="ErrorEllipse" 
        StrokeThickness="0" Fill="Red" 
        Width="{TemplateBinding FontSize}" 
        Height="{TemplateBinding FontSize}" />
    <TextBlock 
        Text="!" 
        FontSize="{TemplateBinding FontSize}" 
        FontWeight="Bold"
        Foreground="White" 
        HorizontalAlignment="Center" />
    <ToolTipService.ToolTip>
       <ToolTip 
           Style="{StaticResource ValidationToolTipStyle}" 
           Content="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}, Path=(Validation.Errors)[0].ErrorContent}" />
        </ToolTip>
    </ToolTipService.ToolTip>
</Grid>

我希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2016-02-11
    • 2013-12-31
    • 2012-05-15
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-15
    相关资源
    最近更新 更多