0

I'm trying to get the entered text from a textBox in WPF with the MVVM method. It works when I am not applying any style on the textBox element.

But when I use this style from the resource dictionary I get empty text :

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<Style TargetType="{x:Type TextBox}" 
       x:Key="YoussefTxtBox">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
                <Border Background="#353340" 
                        CornerRadius="24">
                    <Grid>
                        <Rectangle StrokeThickness="1"/>
                        <TextBox Padding="5" 
                                 Background="Transparent"
                                 VerticalAlignment="Center"
                                 Foreground="#CFCFCF"
                                 Margin="1"
                                 BorderBrush="Transparent"
                                 Text="{TemplateBinding Property=Text}"
                                 BorderThickness="0"
                                 x:Name="InputBox"/>
                        <TextBlock IsHitTestVisible="False"
                                   Text="{TemplateBinding Property=Name}"
                                   VerticalAlignment="Center"
                                   HorizontalAlignment="Left"
                                   Foreground="#F1F0F0"
                                   Margin="10,0,0,0" FontSize="11"
                                   Grid.Column="1">
                            <TextBlock.Style>
                                <Style TargetType="{x:Type TextBlock}">
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding Text , `enter code here`ElementName=InputBox}" Value="">
                                            <Setter Property="Visibility" Value="Visible"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                    <Setter Property="Visibility" Value="Hidden"/>
                                </Style>
                            </TextBlock.Style>
                        </TextBlock>
                        
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

This is the view Model code that I am using to return the string input:

public string _comm;

    public string comm
    {
        get {
            return _comm;
        }

        set
        {
            _comm = value;
            OnPropertyChanged("comm");
        }
        
    }

And this is the view XAML for the TextBox :

<TextBox x:Name="portNom" Height="50" Width="178" 
             VerticalAlignment="Bottom"
             HorizontalAlignment="Left"
             Margin="355,0,0,36"
             Style="{StaticResource YoussefTxtBox}"
             Text="{Binding comm, UpdateSourceTrigger=PropertyChanged}"
             />

I'm new to MVVM concept! And I don't know why this style is making problems!

  • 1
    There should not be a TextBox in the ControlTemplate of a TextBox. See [TextBox Styles and Templates](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/controls/textbox-styles-and-templates?view=netframeworkdesktop-4.8), there should instead be ``. – Clemens Dec 14 '21 at 14:06
  • Besides that, `Text="{TemplateBinding Property=Text}"` doesn't work TwoWay. Replace it by a regular Binding with `RelativeSource={RelativeSource TemplatedParent}`. – Clemens Dec 14 '21 at 14:07
  • Thank you !! I'll try it ... – Youssef Adouiri Alaoui Dec 14 '21 at 14:11

0 Answers0