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!