visual validated texto solo propiedades programar lectura eventos evento caja c# wpf background textbox

c# - validated - Cómo crear un estilo de TextBox de solo lectura



textbox solo lectura visual basic (3)

Intento crear un TextBox que sea de solo lectura y que no tenga animación ni enfoque del mouse con el siguiente estilo xaml. Sin embargo, quiero poder cambiar el color de fondo, pero este estilo no permitirá cambios en el color de fondo. Creo que no estoy entendiendo los conceptos básicos aquí porque parece que no es posible configurar simplemente la propiedad Background, de la misma manera que se puede establecer la propiedad de primer plano: ¿por qué? Y cómo creo un estilo de TextBox que se lee solo y no cambia con el mouse ni la interacción del usuario, pero aún así me permite cambiar los colores de primer plano y de fondo para cada instancia de TextBox.

EDITAR Quizás no fui lo suficientemente explícito, pero hasta donde puedo decir con la propiedad READONLY estándar, el cursor del mouse cambia de forma y todavía es posible seleccionar el texto en el TextBox. No quiero ninguna interacción, ni mouseover, ni focus, nada. Actualizaré mi pregunta para aclarar esto.

Gracias

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


Ready solo significa que no hay interacción con el usuario, incluso el evento mouseover no se activará


Esto es lo que hice al final y parece funcionar. Me interesaría si esta es la forma correcta. Parece que hacer referencia a TextBoxBase hizo el truco y me permite establecer el fondo y otras propiedades de la manera habitual y toda la interacción del usuario permanece deshabilitada. Además, el estilo no cambia para cada estado.

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


Hazlo asi:

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

Puede hacer más desencadenantes tales como:

IsMouseLeave

IsMouseDown

IsMouseMove

IsMouseOver

IsKeyUp

IsKeyDown

cualquier RoutedEvent puede usar y definir las propiedades de los setters para cada uno.

Espero eso ayude... :)