Home > Blockchain >  Textbox style setter breaks functionality
Textbox style setter breaks functionality

Time:10-05

for some reason in my code behind (WPF), the "setter" section makes the text box not function anymore. I can't type in it or add any functionality to it. When I remove the setter section, the text box works. I've seen other people online do it this exact way and it works for them but I'm not sure what I'm doing wrong.

Here's what I have in the main window.

<TextBox Style="{DynamicResource inputBox}" Margin="0,50,125,0"/>

And here's what I have in the code behind

            <Style x:Key="inputBox" TargetType="TextBox">
            <Setter Property="Padding" Value="3"/>
            <Setter Property="Height" Value="35"/>
            <Setter Property="Width" Value="65"/>
            <Setter Property="FontSize" Value="10"/>
            <Setter Property="TextWrapping" Value="Wrap"/>
            <Setter Property="Text" Value=""/>
            <Setter Property="HorizontalAlignment" Value="Center"/>
            <Setter Property="VerticalAlignment" Value="Top"/>

            <!--This setter somehow breaks the text box-->
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TextBox}">
                        <Border Background="Transparent" CornerRadius="5" BorderThickness="1" BorderBrush="Black">
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

Thanks

CodePudding user response:

Your template only shows the border. Not the content. It needs a PART_ContentHost Do yourself a favor and copy the existing template and edit that. You can do this on the XAML designer. For example, here is what it looks like when I copy it on mine:

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">
                    <Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="True">
                        <ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Opacity" TargetName="border" Value="0.56"/>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource TextBox.MouseOver.Border}"/>
                        </Trigger>
                        <Trigger Property="IsKeyboardFocused" Value="true">
                            <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource TextBox.Focus.Border}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
  • Related