ErrorTemplate的不同ControlTemplates

时间:2015-03-12 10:12:47

标签: c# wpf binding controltemplate idataerrorinfo

在我的应用程序中,我有一个ControlTemplate,我用它来表明TextBox的输入无效。 ControlTemplate定义为:

<ControlTemplate x:Key="TextBoxErrorTemplate" TargetType="Control">
    <Grid ClipToBounds="False">
        <Border BorderBrush="Red" BorderThickness="1" Margin="-1">
            <AdornedElementPlaceholder Name="adornedElement" />
        </Border>
        <Image HorizontalAlignment="Right" VerticalAlignment="Top"
               Width="16" Height="16" Margin="0,-9,-8,0" Source="pack://application:,,,/UI.Resources;component/Graphics/Error_16_16.png"
               ToolTip="{Binding ElementName=adornedElement, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}" />
    </Grid>
</ControlTemplate>

用法是:

<TextBox Grid.Row="1" Margin="0,5"
         Text="{Binding UserGroup.Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"
         Validation.ErrorTemplate="{StaticResource TextBoxErrorTemplate}"/>

UserGroup类实现IDataErrorInfo - 接口。该课程的一小部分看起来像:

public class UserGroup
{
    public string Name
    {
        get { return Get(() => Name); }
        set { Set(() => Name, value); }
    }

    public bool IsDuplicate
    {
        get { return Get(() => IsDuplicate); }
        set { Set(() => IsDuplicate, value); }
    }

    public bool IsSimilar
    {
        get { return Get(() => IsSimilar); }
        set { Set(() => IsSimilar, value);}
    }
}

IDataErrorInfo所需的getter实现如下:

public string this[string columnName]
{
    get
    {
        string result = string.Empty;
        string namePropertyName = GetPropertyNameFromExpression(() => Name);
        string isDuplicatePropertyName = GetPropertyNameFromExpression(() => IsDuplicate);
        string isSimilarPropertyName = GetPropertyNameFromExpression(() => IsSimilar);

        if (columnName == namePropertyName)
        {
            if(IsSimilar)
            {
                result = "Be careful with similar group-names!";
                if (!Error.Contains(isSimilarPropertyName))
                {
                    Error += isSimilarPropertyName;
                }
            }
            else
            {
                Error = Error.Replace(isSimilarPropertyName, string.Empty);
            }

            if (IsDuplicate)
            {
                result = "Duplicate names are not allowed!";
                if (!Error.Contains(isDuplicatePropertyName))
                {
                    Error += isDuplicatePropertyName;
                }
            }           
            else
            {
                Error = Error.Replace(isDuplicatePropertyName, string.Empty);
            }
        }       
        return result;
    }
}

因此,如果IsSimilarIsDuplicate为真,则将使用TextBoxErrorTemplate - ControlTemplate

我想要的是仅当IsDuplicate等于true时才应使用TextBoxErrorTemplate。

如果IsSimilar等于true,我想使用以下ControlTemplate

<ControlTemplate x:Key="TextBoxWarningTemplate" TargetType="Control">
    <Grid ClipToBounds="False">
        <Border BorderBrush="Orange" BorderThickness="1" Margin="-1">
            <AdornedElementPlaceholder Name="adornedElement" />
        </Border>
        <Image HorizontalAlignment="Right" VerticalAlignment="Top"
               Width="16" Height="16" Margin="0,-9,-8,0" Source="pack://application:,,,/UI.Resources;component/Graphics/Warning_16_16.png"
               ToolTip="{Binding ElementName=adornedElement, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}" />
    </Grid>
</ControlTemplate>

有没有办法实现这个目标?我的第一种方法是使用Border覆盖TextBox,并且仅在IsSimilar为真时显示它,但这看起来并不好......

我已阅读TemplateSelector,但仅限于DataGridTemplateColumn

的背景下

1 个答案:

答案 0 :(得分:0)

我自己解决了。解决方案只有一个ControlTemplate,其中ImageSourceBorderBrush绑定到UserGroup-Property,并且决定在两个转换器中完成。

ControlTemplate看起来像:

 <ControlTemplate x:Key="TextBoxErrorTemplate" TargetType="Control">
    <Grid ClipToBounds="False">
        <Border BorderThickness="1" Margin="-1" 
                BorderBrush="{Binding DataContext.UserGroup, Converter={converters:UserGroupToBrushConverter}, 
                              RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}">
            <AdornedElementPlaceholder Name="adornedElement" />
        </Border>
        <Image HorizontalAlignment="Right" VerticalAlignment="Top"
               Width="16" Height="16" Margin="0,-9,-8,0" 
               Source="{Binding DataContext.UserGroup, Converter={converters:UserGroupToImageSourceConverter},
                        RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"
               ToolTip="{Binding ElementName=adornedElement, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}" />
    </Grid>
</ControlTemplate>