Dmirty
Topic Author
Posts: 9
Joined: 19 Oct 2022, 19:50

Can't apply a style to TextBox

19 Apr 2024, 07:10

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>
 
antdeceive
Posts: 1
Joined: 19 Apr 2024, 10:53

Re: Can't apply a style to TextBox

19 Apr 2024, 11:01

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.
 
User avatar
sfernandez
Site Admin
Posts: 3003
Joined: 22 Dec 2011, 19:20

Re: Can't apply a style to TextBox

22 Apr 2024, 19:02

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.
 
Dmirty
Topic Author
Posts: 9
Joined: 19 Oct 2022, 19:50

Re: Can't apply a style to TextBox

28 Apr 2024, 06:54

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!

Who is online

Users browsing this forum: Google [Bot] and 1 guest