自定义工具提示验证错误WPF

时间:2017-01-05 14:57:38

标签: c# wpf

我正在尝试创建自定义工具提示。问题是我无法显示错误文本。这段代码完美无缺(简单的工具提示)

<Style TargetType="{x:Type TextBox}"
   BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Validation.ErrorTemplate">
    <Setter.Value>
        <ControlTemplate>
            <Grid>
                <Polygon Fill="Red"
                         Margin="0,2,2,0"
                         Points="10,10 10,0 0,0"
                         VerticalAlignment="Top"
                         HorizontalAlignment="Right"
                         ToolTip="{Binding ElementName=adorner, Path=AdornedElement.(Validation.Errors).CurrentItem.ErrorContent}">
                </Polygon>
                <AdornedElementPlaceholder x:Name="adorner" />
            </Grid>
        </ControlTemplate>
    </Setter.Value>
</Setter>

但是这段代码不起作用,没有显示错误

<Style TargetType="{x:Type TextBox}"
   BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Validation.ErrorTemplate">
    <Setter.Value>
        <ControlTemplate>
            <Grid>
                <Polygon Fill="Red"
                         Margin="0,2,2,0"
                         Points="10,10 10,0 0,0"
                         VerticalAlignment="Top"
                         HorizontalAlignment="Right">
                    <Polygon.ToolTip>
                        <ToolTip Content="{Binding ElementName=adorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"
                                 BorderThickness="1"
                                 Foreground="White"
                                 Background="Red" />
                    </Polygon.ToolTip>
                </Polygon>
                <AdornedElementPlaceholder x:Name="adorner" />
            </Grid>
        </ControlTemplate>
    </Setter.Value>
</Setter>

2 个答案:

答案 0 :(得分:1)

Tooltip本身和AdornedElementPlaceholder位于不同的名称范围内,因此使用ElementName进行绑定将不起作用。

但您可以将Polygon的Tag属性设置为ErrorContent,并将Tooltip的Content属性绑定到其PlacementTarget的Tag属性(即Polygon)。这有效:

<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <Grid>
                    <Polygon Fill="Red"
                         Margin="0,2,2,0"
                         Points="10,10 10,0 0,0"
                         VerticalAlignment="Top"
                         HorizontalAlignment="Right"
                         Tag="{Binding ElementName=adorner, Path=AdornedElement.(Validation.Errors).CurrentItem.ErrorContent}">
                        <Polygon.ToolTip>
                            <ToolTip Content="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}"
                                 BorderThickness="1"
                                 Foreground="White"
                                 Background="Red" />
                        </Polygon.ToolTip>
                    </Polygon>
                    <AdornedElementPlaceholder x:Name="adorner" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

答案 1 :(得分:0)

(Validation.Errors)[0]的调用将很麻烦,因为您从列表中引用特定对象将更新并更改因此破坏绑定。

使用(Validation.Errors).CurrentItem.的第一个示例更适合,因为它与您的第一个实现相匹配。

几年前我遇到了这个问题,基本上从不在绑定中使用索引,除非你绝对100%确定它不会改变。