如何创建只读TextBox样式

时间:2014-04-25 22:28:31

标签: c# wpf background textbox

我正在尝试使用以下xaml样式创建一个只读且没有动画或鼠标焦点的TextBox。但是,我希望能够更改背景颜色,但此样式不允许更改背景颜色。我想我不理解这里的基本概念,因为它似乎不可能简单地设置Background属性本身,就像设置foreground属性一样 - 为什么这样做以及如何创建一个读取的TextBox样式只有并且不会因任何鼠标移动或用户交互而改变,但仍允许我更改TextBox的每个实例的前景色和背景色。

EDIT 也许我不够明确,但据我所知,标准READONLY属性鼠标光标改变形状,仍然可以选择TextBox中的文本。我想要没有任何互动,没有鼠标悬停,没有焦点,没有任何东西。将更新我的问题以明确这一点。

由于

<Style x:Key="readOnlyTextBoxColor1" TargetType="{x:Type TextBox}">
    <Setter Property="SnapsToDevicePixels" Value="True"/>
    <Setter Property="OverridesDefaultStyle" Value="True"/>
    <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    <Setter Property="MinWidth" Value="120"/>
    <Setter Property="MinHeight" Value="20"/>
    <Setter Property="AllowDrop" Value="False"/>
    <Setter Property="BorderThickness" Value="0"/>
    <Setter Property="FontFamily" Value="Arial"/>
    <Setter Property="FontSize" Value="36"/>
    <Setter Property="FontWeight" Value="Bold"/>
    <Setter Property="TextWrapping" Value="Wrap"/>
    <Setter Property="IsReadOnly" Value="True"/>
    <Setter Property="IsEnabled" Value="False"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="HorizontalContentAlignment" Value="Center"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
                <Border 
  Name="Border"
  BorderThickness="0" >
                    <ScrollViewer Margin="0" x:Name="PART_ContentHost"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter TargetName="Border" Property="Background" Value="{StaticResource ThemeSolidColorBrushColor1}"/>
                        <Setter TargetName="Border" Property="BorderBrush" Value="Black"/>
                        <Setter Property="Foreground" Value="{StaticResource ThemeSolidColorBrushWhite}"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="True">
                        <Setter TargetName="Border" Property="Background" Value="{StaticResource ThemeSolidColorBrushColor1}"/>
                        <Setter TargetName="Border" Property="BorderBrush" Value="Black"/>
                        <Setter Property="Foreground" Value="{StaticResource ThemeSolidColorBrushDarkGray}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

3 个答案:

答案 0 :(得分:1)

这是我最后所做的,似乎有效。如果这是正确的方法我会感兴趣。似乎引用TextBoxBase完成了这个技巧,并允许我以通常的方式设置Background和其他属性,并且所有用户交互仍然被禁用。此外,每个州的风格都不会改变。

<Style x:Key="staticTextBox" TargetType="{x:Type TextBox}">
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="OverridesDefaultStyle" Value="True" />
    <Setter Property="KeyboardNavigation.TabNavigation" Value="None" />
    <Setter Property="FocusVisualStyle"  Value="{x:Null}" />
    <Setter Property="MinWidth"  Value="120" />
    <Setter Property="MinHeight" Value="20" />
    <Setter Property="AllowDrop"  Value="false" />
    <Setter Property="IsReadOnly"  Value="true" />
    <Setter Property="IsEnabled"  Value="false" />
    <Setter Property="FontSize" Value="36"/>
    <Setter Property="FontFamily" Value="Arial"/>
    <Setter Property="FontWeight" Value="Bold"/>
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="BorderThickness" Value="0"/>
    <Setter Property="TextAlignment" Value="Center"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBoxBase}">
                <Border Name="Border"
        CornerRadius="2"
        Padding="2"
        BorderThickness="0" Background="{TemplateBinding Background}">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal" />
                            <VisualState x:Name="Disabled">
                            </VisualState>
                            <VisualState x:Name="ReadOnly">
                            </VisualState>
                            <VisualState x:Name="MouseOver" />
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <ScrollViewer Margin="0"
                x:Name="PART_ContentHost" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

答案 1 :(得分:0)

就绪只表示没有与用户互动, 即使是鼠标悬停事件也不会被解雇

答案 2 :(得分:0)

这样做:

   <Style x:Key="readOnlyTextBoxColor1" TargetType="{x:Type TextBox}">
   <Setter Property="SnapsToDevicePixels" Value="True"/>
   <Setter Property="OverridesDefaultStyle" Value="True"/>
  <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="MinWidth" Value="120"/>
<Setter Property="MinHeight" Value="20"/>
<Setter Property="AllowDrop" Value="False"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="FontSize" Value="36"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="TextWrapping" Value="Wrap"/>
<Setter Property="IsReadOnly" Value="True"/>
<Setter Property="IsEnabled" Value="False"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type TextBox}">
            <Border Name="Border" BorderThickness="0" >
                <ScrollViewer Margin="0" x:Name="PART_ContentHost"/>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter TargetName="Border" Property="Background" Value="{StaticResource ThemeSolidColorBrushColor1}"/>
                    <Setter TargetName="Border" Property="BorderBrush" Value="Black"/>
                    <Setter Property="Foreground" Value="{StaticResource ThemeSolidColorBrushWhite}"/>
                </Trigger>
                <Trigger Property="IsEnabled" Value="True">
                    <Setter TargetName="Border" Property="Background" Value="{StaticResource ThemeSolidColorBrushColor1}"/>
                    <Setter TargetName="Border" Property="BorderBrush" Value="Black"/>
                    <Setter Property="Foreground" Value="{StaticResource ThemeSolidColorBrushDarkGray}"/>
                </Trigger>
   <Trigger Property="IsMouseOver" Value="True">
                    <Setter TargetName="Border" Property="Background" Value="{x:Null}"/>
                    <Setter TargetName="Border" Property="BorderBrush" Value="{x:Null}"/>
                    <Setter Property="Foreground" Value="YourColorHere"/>
                    <Setter Property="Cursor" Value="Default"/>
                    <Setter Property="Background" Value="YourColorHere"/>
                </Trigger>

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

你可以制造更多触发器:

IsMouseLeave

IsMouseDown

IsMouseMove

IsMouseOver

IsKeyUp

IsKeyDown

您可以使用的任何RoutedEvent,并为每个RoutedEvent定义setter属性。

希望它有助于......:)