Page 1 of 1

Can't apply a style to TextBox

Posted: 19 Apr 2024, 07:10
by Dmirty
Hello there. This code works in Blend, but doesn't in XamlToy/Noesis app.
How to fix it (textBox2)?
<UserControl 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" mc:Ignorable="d"
        xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
        xmlns:noesis="clr-namespace:NoesisGUIExtensions;assembly=Noesis.GUI.Extensions"
        d:DesignHeight="384" d:DesignWidth="285" Width="285" Height="384">
    <UserControl.Resources>
        <ResourceDictionary>
            <Style x:Key="DarkInput" TargetType="{x:Type TextBox}">
                <Setter Property="Background" Value="#FF221E22"/>
                <Setter Property="BorderBrush" Value="#FF5B5B5B"/>
                <Setter Property="Foreground" Value="White"/>
                <Setter Property="VerticalContentAlignment" Value="Center"/>
                <Setter Property="HorizontalContentAlignment" Value="Center"/>
                <Setter Property="MaxLines" Value="1"/>
                <Setter Property="Padding" Value="0"/>
            </Style>
        </ResourceDictionary>
    </UserControl.Resources>

    <Grid>
        <StackPanel>
            <TextBox x:Name="textBox1" Text="{Binding ValueR}" Height="25" MaxLength="3" Background="#FF221E22" BorderBrush="#FF5B5B5B" Foreground="White" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" MaxLines="1" Margin="0,0,0,10" Padding="0"/>
            <TextBox x:Name="textBox2" Text="{Binding ValueR}" Height="25" MaxLength="3" Style="{StaticResource DarkInput}"/>
        </StackPanel>
    </Grid>
</UserControl>

Re: Can't apply a style to TextBox

Posted: 19 Apr 2024, 11:01
by antdeceive
It looks like the issue might be related to the style being applied to textBox2.snake game Since you mentioned that the code works in Blend but not in XamlToy/Noesis app, there could be a compatibility issue with the way styles are handled or applied in those environments.
One potential solution could be to remove the Style attribute from textBox2 and manually apply the style in the Loaded event handler of the UserControl.

Re: Can't apply a style to TextBox

Posted: 22 Apr 2024, 19:02
by sfernandez
Hi, if you apply a local style without a template to a TextBox, your style should specify the BasedOn to inherit the template from the theme styles.
<Style x:Key="DarkInput" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
But I suggest you provide your own template, because the default one can have over or focus states that don't match the rest of your application theme.

Re: Can't apply a style to TextBox

Posted: 28 Apr 2024, 06:54
by Dmirty
Hi, if you apply a local style without a template to a TextBox, your style should specify the BasedOn to inherit the template from the theme styles.
<Style x:Key="DarkInput" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
But I suggest you provide your own template, because the default one can have over or focus states that don't match the rest of your application theme.
I see. Thank you!