IsReadOnly签入控制模板触发wpf

IsReadOnly签入控制模板触发wpf,wpf,textbox,controltemplate,readonly-attribute,Wpf,Textbox,Controltemplate,Readonly Attribute,我想在文本框中选中IsReadOnly 这是我的文本框风格 但我不能应用“IsReadOnly”属性 它在Property=“IsReadOnly”和 “IsReadOnly”成员无法识别或无法访问发生错误 为什么? 那我怎么修呢 <!-- textbox Style --> <Style x:Key="TextInputStyle" TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}"

我想在文本框中选中IsReadOnly

这是我的文本框风格

但我不能应用“IsReadOnly”属性

它在Property=“IsReadOnly”和

“IsReadOnly”成员无法识别或无法访问发生错误

为什么?

那我怎么修呢

<!-- textbox Style -->
        <Style x:Key="TextInputStyle" TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
            <Setter Property="FontSize" Value="12"/>
            <Setter Property="Margin" Value="5,5,5,5"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Border x:Name="bg" BorderBrush="#FF7F98DC" BorderThickness="1" Background="White">
                            <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="BorderBrush" TargetName="bg" Value="#FF7E97F0"/>
                                <Setter Property="BorderThickness" TargetName="bg" Value="2"/>
                            </Trigger>
                            <Trigger Property="IsFocused" Value="True">
                                <Setter Property="BorderBrush" TargetName="bg" Value="DarkBlue"/>
                                <Setter Property="BorderThickness" TargetName="bg" Value="2"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Foreground" Value="Gray"/>
                                <Setter Property="Background" TargetName="PART_ContentHost" Value="LightGray"/>
                            </Trigger>

                            <!--Here is an Error code! -->
                            <Trigger Property="IsReadOnly" Value="True"> 
                                <Setter Property="Foreground" Value="Gray"/>
                                <Setter Property="Background" TargetName="PART_ContentHost" Value="LightGray"/>
                            </Trigger>

                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

使用以下方法:

<ControlTemplate TargetType="{x:Type TextBox}">

完整示例:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp21"
        xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" x:Class="WpfApp21.MainWindow"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style x:Key="TextInputStyle" TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
            <Setter Property="FontSize" Value="12"/>
            <Setter Property="Margin" Value="5,5,5,5"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TextBox}">
                        <Border x:Name="bg" BorderBrush="#FF7F98DC" BorderThickness="1" Background="White">
                            <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="BorderBrush" TargetName="bg" Value="#FF7E97F0"/>
                                <Setter Property="BorderThickness" TargetName="bg" Value="2"/>
                            </Trigger>
                            <Trigger Property="IsFocused" Value="True">
                                <Setter Property="BorderBrush" TargetName="bg" Value="DarkBlue"/>
                                <Setter Property="BorderThickness" TargetName="bg" Value="2"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Foreground" Value="Gray"/>
                                <Setter Property="Background" TargetName="PART_ContentHost" Value="LightGray"/>
                            </Trigger>

                            <!--Here is an Error code! -->
                            <Trigger Property="IsReadOnly" Value="True">
                                <Setter Property="Foreground" Value="Gray"/>
                                <Setter Property="Background" TargetName="PART_ContentHost" Value="LightGray"/>
                            </Trigger>

                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <TextBox IsReadOnly="False" Style="{StaticResource TextInputStyle}" Text="FFFFFFFFFFFFFFFF"/>
            <TextBox IsReadOnly="True" Style="{StaticResource TextInputStyle}" Text="TTTTTTTTTTTTTTT"/>
        </StackPanel>
    </Grid>
</Window>

使用以下方法:

<ControlTemplate TargetType="{x:Type TextBox}">

完整示例:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp21"
        xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" x:Class="WpfApp21.MainWindow"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style x:Key="TextInputStyle" TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
            <Setter Property="FontSize" Value="12"/>
            <Setter Property="Margin" Value="5,5,5,5"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TextBox}">
                        <Border x:Name="bg" BorderBrush="#FF7F98DC" BorderThickness="1" Background="White">
                            <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="BorderBrush" TargetName="bg" Value="#FF7E97F0"/>
                                <Setter Property="BorderThickness" TargetName="bg" Value="2"/>
                            </Trigger>
                            <Trigger Property="IsFocused" Value="True">
                                <Setter Property="BorderBrush" TargetName="bg" Value="DarkBlue"/>
                                <Setter Property="BorderThickness" TargetName="bg" Value="2"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Foreground" Value="Gray"/>
                                <Setter Property="Background" TargetName="PART_ContentHost" Value="LightGray"/>
                            </Trigger>

                            <!--Here is an Error code! -->
                            <Trigger Property="IsReadOnly" Value="True">
                                <Setter Property="Foreground" Value="Gray"/>
                                <Setter Property="Background" TargetName="PART_ContentHost" Value="LightGray"/>
                            </Trigger>

                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <TextBox IsReadOnly="False" Style="{StaticResource TextInputStyle}" Text="FFFFFFFFFFFFFFFF"/>
            <TextBox IsReadOnly="True" Style="{StaticResource TextInputStyle}" Text="TTTTTTTTTTTTTTT"/>
        </StackPanel>
    </Grid>
</Window>