ControlTemplate for ComboBox周围的边框

时间:2014-08-08 06:08:38

标签: c# wpf combobox styles controltemplate

我有以下ControlTemplate来验证ComboBox

<ControlTemplate x:Key="ComboBoxHighlightTemplate" TargetType="Control">
    <Grid ClipToBounds="False">
        <Border BorderBrush="{StaticResource fokusBrush}" BorderThickness="2.5" Margin="-1" Visibility="{Binding ElementName=adornedElementHighligh,
            Path=AdornedElement.(Validation.Errors)[0].ErrorContent, Converter={StaticResource inverseErrorContentVisibilityConverter}}">
            <AdornedElementPlaceholder Name="adornedElementHighligh"/>
        </Border>

        <Border BorderBrush="Red" BorderThickness="1" Margin="-1" Visibility="{Binding ElementName=adornedElement,
            Path=AdornedElement.(Validation.Errors)[0].ErrorContent, Converter={StaticResource errorContentToErrorVisibilityConverter}}">
            <AdornedElementPlaceholder Name="adornedElement" />
        </Border>
        <Image HorizontalAlignment="Right" VerticalAlignment="Top" Visibility="{Binding ElementName=adornedElement,
            Path=AdornedElement.(Validation.Errors)[0].ErrorContent, Converter={StaticResource errorContentToErrorVisibilityConverter}}"
                Width="16" Height="16" Margin="0,-9,-9,0" Source="{x:Static helper:ImageHelper.ErrorImage}"
                ToolTip="{Binding ElementName=adornedElement, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}" />
    </Grid>
</ControlTemplate>

根据ErrorContent,我决定在Border周围绘制哪个ComboBox

Converter ErrorContentToErrorVisibilityConverter看起来像

internal class ErrorContentToErrorVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is string)
        {
            if (((string) value).EndsWith("not an error"))
                return Visibility.Hidden;
        }
        return Visibility.Visible;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

一切都很好。但是如果我在ComboBox中产生错误以便显示ControlTemplate for Errors,则绘制边框的边界不在ComboBox的“实际”边界。

ControlTemplate的用法如下:

1 个答案:

答案 0 :(得分:1)

这是一个示例模板,我确实尝试为您简化它

    <ControlTemplate x:Key="ComboBoxHighlightTemplate"
                     TargetType="Control">
        <Grid ClipToBounds="False">
            <AdornedElementPlaceholder Name="adornedElement" />
            <Border BorderBrush="Red"
                    BorderThickness="1"
                    x:Name="errorBorder" />
            <Image HorizontalAlignment="Right"
                   VerticalAlignment="Top"
                   x:Name="image"
                   Width="16"
                   Height="16"
                   Margin="0,-9,-9,0"
                   Source="{x:Static helper:ImageHelper.ErrorImage}"
                   ToolTip="{Binding ElementName=adornedElement, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}" />
        </Grid>
        <ControlTemplate.Triggers>
            <DataTrigger Binding="{Binding AdornedElement.(Validation.Errors)[0].ErrorContent, ElementName=adornedElement,Converter={StaticResource errorContentToErrorVisibilityConverter}}"
                         Value="Hidden">
                <Setter Property="BorderBrush"
                        TargetName="errorBorder"
                        Value="{StaticResource fokusBrush}" />
                <Setter Property="BorderThickness"
                        TargetName="errorBorder"
                        Value="2.5" />
                <Setter Property="Visibility"
                        TargetName="image"
                        Value="Collapsed" />
            </DataTrigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

尝试一下,让我知道你的结果有多接近,我们也可以进一步简化触发器,我是按照假设做的。