如何创建一个只读的TextBox样式 [英] How to create a read only TextBox style

查看:73
本文介绍了如何创建一个只读的TextBox样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下xaml样式创建一个TextBox,该TextBox是只读的并且没有动画或鼠标焦点.但是,我希望能够更改背景颜色,但是此样式不允许更改背景颜色.我想我不了解这里的基本概念,因为似乎无法简单地设置background属性本身,就可以以设置前台属性的方式进行设置-为什么这样做以及如何创建读取的TextBox样式唯一,并且不会随着鼠标悬停或用户交互而改变,但仍然允许我更改TextBox的每个实例的前景和背景颜色.

I am trying to create a TextBox which is readonly and which has no animation or mouse focus using the following xaml style. However I want to be able to change the background colour but this style will not allow changes to the background colour. I think I am not understanding the basic concepts here because it seems it is not possible to simply set the Background property itself, in the same way the foreground property can be set - why is that and how do I create a TextBox style that is read only and does not change with any mouse over or user interaction but still allows me to change the Foreground and Background colours for each instance of the TextBox.

编辑 也许我不够明确,但是据我所知,使用标准的READONLY属性,鼠标光标会改变形状,并且仍然可以在TextBox中选择文本.我根本不需要任何交互,没有鼠标悬停,没有焦点,什么也没有.将更新我的问题以使内容更清楚.

EDIT Perhaps I was not explicit enough but as far as I can tell with the standard READONLY property the mouse cursor changes shape and it is still possible to select the text in the TextBox. I want no interaction at all, no mouseover, no focus, nothing. Will update my question to make this clear.

谢谢

<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>

推荐答案

这是我最后所做的,而且似乎可以正常工作.如果这是正确的方法,我会很感兴趣.似乎引用TextBoxBase可以达到目的,并允许我以通常的方式设置Background和其他属性,并且所有用户交互都保持禁用状态.同样,样式不会针对每种状态而改变.

This is what I did in the end and it seems to work. I would be interested if this is the correct way. It seems that referencing TextBoxBase did the trick and allows me to set Background and other properties in the usual way and all user interaction remains disabled. Also the style does not change for each state.

<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>

这篇关于如何创建一个只读的TextBox样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆