在WPF中覆盖样式定义的ControlTemplate Border

时间:2014-05-28 20:25:17

标签: wpf

我们的应用程序为TextBox定义了一个sytle,其中包含一个带有Border定义的ControlTemplate

    <Style TargetType="{x:Type TextBox}">
    <Setter Property="OverridesDefaultStyle" Value="True" />
    <Setter Property="Foreground" Value="White" />
    <Setter Property="Background" Value="Black" />
    ...
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBoxBase}">
                <Border x:Name="myBorder"
                        Background="{TemplateBinding Background}"
                        BorderBrush="{StaticResource DefaultBorderBrush}"
                        BorderThickness="2">

                    <ScrollViewer x:Name="myContentHost"
                                  VerticalAlignment="Center"
                                  Background="{TemplateBinding Background}"
                                  BorderBrush="{StaticResource DefaultBorderBrush}"
                                  BorderThickness="1"
                                  Foreground="{TemplateBinding Background}" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我有几个我想要的TextBox,没有边框,但有其他定义的样式。我无法通过将BorderBrush设置为Transparent或null来清除它。有没有一种方法来覆盖边界?

1 个答案:

答案 0 :(得分:2)

您需要在样式中的Setter中初始化BorderBrush,并在TemplateBinding<Border>元素中使用<ScrollViewer>作为BorderBrushes。这是一个例子:

<Window x:Class="WpfApplication8.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <SolidColorBrush x:Key="DefaultBorderBrush" Color="Red"/>
        <Style TargetType="{x:Type TextBox}">
            <Setter Property="OverridesDefaultStyle" Value="True" />
            <Setter Property="Foreground" Value="White" />
            <Setter Property="Background" Value="Black" />
            <Setter Property="BorderBrush" Value="{StaticResource DefaultBorderBrush}" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TextBoxBase}">
                        <Border x:Name="myBorder"
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="2">
                            <ScrollViewer x:Name="PART_ContentHost"
                                  VerticalAlignment="Center"
                                  Background="{TemplateBinding Background}"
                                  BorderBrush="{TemplateBinding BorderBrush}"
                                  BorderThickness="1"
                                  Foreground="{TemplateBinding Foreground}" />
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid Margin="4">
        <TextBox Width="100" Height="20" HorizontalAlignment="Left" VerticalAlignment="Top" Text="txt1"/>
        <TextBox Width="100" Height="20" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,30,0,0" Text="txt2" BorderBrush="Transparent"/>
    </Grid>
</Window>

结果如下:

screen shot