WPF文本框样式问题

WPF文本框样式问题,wpf,xaml,Wpf,Xaml,有人能解释一下为什么CornerRadius的文本框资源样式工作得很好,但是BorderThickness和BorderBrush没有效果吗 <TextBox Text="TextBox with CornerRadius but no thickness and color" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" VerticalA

有人能解释一下为什么
CornerRadius
的文本框资源样式工作得很好,但是BorderThickness和BorderBrush没有效果吗

<TextBox Text="TextBox with CornerRadius but no thickness and color" 
         HorizontalContentAlignment="Center"
         VerticalContentAlignment="Center"
         VerticalAlignment="Center" Width="500" Height="100">
    <TextBox.Resources>
        <Style TargetType="{x:Type Border}">                
            <Setter Property="CornerRadius" Value="30" />
            <Setter Property="BorderThickness" Value="30" />
            <Setter Property="BorderBrush" Value="Red" />
        </Style>
    </TextBox.Resources>
</TextBox>

原因是在标准的
TextBoxBase
模板中,边框控件的属性
BorderThickness
BorderBrush
绑定到
TextBox
本身的相同属性

以下是样式中使用的标准ControlTemplate:

<ControlTemplate TargetType="{x:Type TextBoxBase}">
    <cbd:ClassicBorderDecorator x:Name="Bd" BorderStyle="Sunken" Background="{TemplateBinding Control.Background}" BorderThickness="{TemplateBinding Control.BorderThickness}" BorderBrush="{TemplateBinding Control.BorderBrush}">
        <ScrollViewer Name="PART_ContentHost" />
    </cbd:ClassicBorderDecorator>
    <ControlTemplate.Triggers>
        <Trigger Property="UIElement.IsEnabled" Value="False">
            <Setter TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" Property="theme:ClassicBorderDecorator.Background" />
            <Setter Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" Property="Control.Foreground" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

因此,为了达到目标,可以通过以下方式设置这些属性:

<TextBox Text="TextBox with CornerRadius but no thickness and color" 
        HorizontalContentAlignment="Center" BorderThickness="30" BorderBrush="Red"
        VerticalContentAlignment="Center"
        VerticalAlignment="Center" Width="500" Height="100">
        <TextBox.Resources>
        <Style TargetType="{x:Type Border}">
            <Setter Property="CornerRadius" Value="30" />
        </Style>
    </TextBox.Resources>
</TextBox>


我希望它能帮助您。

原因是在标准的
TextBoxBase
模板中,边框控件的属性
BorderThickness
BorderBrush
绑定到
TextBox
本身的相同属性

以下是样式中使用的标准ControlTemplate:

<ControlTemplate TargetType="{x:Type TextBoxBase}">
    <cbd:ClassicBorderDecorator x:Name="Bd" BorderStyle="Sunken" Background="{TemplateBinding Control.Background}" BorderThickness="{TemplateBinding Control.BorderThickness}" BorderBrush="{TemplateBinding Control.BorderBrush}">
        <ScrollViewer Name="PART_ContentHost" />
    </cbd:ClassicBorderDecorator>
    <ControlTemplate.Triggers>
        <Trigger Property="UIElement.IsEnabled" Value="False">
            <Setter TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" Property="theme:ClassicBorderDecorator.Background" />
            <Setter Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" Property="Control.Foreground" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

因此,为了达到目标,可以通过以下方式设置这些属性:

<TextBox Text="TextBox with CornerRadius but no thickness and color" 
        HorizontalContentAlignment="Center" BorderThickness="30" BorderBrush="Red"
        VerticalContentAlignment="Center"
        VerticalAlignment="Center" Width="500" Height="100">
        <TextBox.Resources>
        <Style TargetType="{x:Type Border}">
            <Setter Property="CornerRadius" Value="30" />
        </Style>
    </TextBox.Resources>
</TextBox>


我希望它能帮助您。

文本框模板中边框元素的BorderThickness和BorderBrush绑定到模板控件的相应属性,即文本框:

<ControlTemplate TargetType="TextBox">
    <Border BorderBrush="{TemplateBinding BorderBrush}"
            BorderThickness="{TemplateBinding BorderThickness}" ...>
        ...            
    </Border>
</ControlTemplate>

...            
这些绑定覆盖任何边框样式设置器中的值

您应该以文本框样式设置值:

<TextBox Text="TextBox with CornerRadius but no thickness and color" 
        HorizontalContentAlignment="Center"
        VerticalContentAlignment="Center"
        VerticalAlignment="Center" Width="500" Height="100">
    <TextBox.Style>
        <Style TargetType="TextBox">
            <Style.Resources>
                <Style TargetType="Border">
                    <Setter Property="CornerRadius" Value="30"/>
                </Style>
            </Style.Resources>
            <Setter Property="BorderThickness" Value="30"/>
            <Setter Property="BorderBrush" Value="Red"/>
        </Style>
    </TextBox.Style>
</TextBox>

文本框模板中边框元素的BorderThickness和BorderBrush绑定到模板控件的相应属性,即文本框:

<ControlTemplate TargetType="TextBox">
    <Border BorderBrush="{TemplateBinding BorderBrush}"
            BorderThickness="{TemplateBinding BorderThickness}" ...>
        ...            
    </Border>
</ControlTemplate>

...            
这些绑定覆盖任何边框样式设置器中的值

您应该以文本框样式设置值:

<TextBox Text="TextBox with CornerRadius but no thickness and color" 
        HorizontalContentAlignment="Center"
        VerticalContentAlignment="Center"
        VerticalAlignment="Center" Width="500" Height="100">
    <TextBox.Style>
        <Style TargetType="TextBox">
            <Style.Resources>
                <Style TargetType="Border">
                    <Setter Property="CornerRadius" Value="30"/>
                </Style>
            </Style.Resources>
            <Setter Property="BorderThickness" Value="30"/>
            <Setter Property="BorderBrush" Value="Red"/>
        </Style>
    </TextBox.Style>
</TextBox>


很抱歉,这一个是正确的,另一个表示基本相同,但引入了非默认/标准元素+1令人担忧的是,这一个是正确的,另一个说的基本相同,但引入了非默认/标准元素+1.