Pregunta

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.

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.

Thanks

<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>
¿Fue útil?

Solución

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>

Otros consejos

Ready only means no interaction to the user, even the mouseover event will not be fired

do it like this:

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

You Can Make More Triggers Such As:

IsMouseLeave

IsMouseDown

IsMouseMove

IsMouseOver

IsKeyUp

IsKeyDown

any RoutedEvent you can use and define setters properties for each.

Hope It Helps... :)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top