1

I have this XAML code that creates a txtbox with a placeholder text, that appears and disapears if it's focused or not.

But right now, I need to translate the aplication to 2 languages with a button. That button simply sets a global variable to "EN" for english and "ES" for spanish.

How can I adapte the code, so depending on that variable (written in code behind) the text "Please, write the reason for your request" changes?

This is the code working right now:

<TextBox x:Name="txt_reasons" ScrollViewer.VerticalScrollBarVisibility="Auto" HorizontalAlignment="Left" Height="74" Margin="82,247,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="344" TabIndex="4" MaxLength="150" TextChanged="txt_reasons_TextChanged" IsEnabled="False" IsHitTestVisible="True">
    <TextBox.Style>
        <Style TargetType="TextBox" xmlns:sys="clr-namespace:System;assembly=mscorlib">
            <Style.Resources>
                <VisualBrush x:Key="CueBannerBrush" AlignmentX="Left" AlignmentY="Top" Stretch="None">
                    <VisualBrush.Visual>
                        <Label Content="Please, write the reason for your request" Foreground="Gray" ClipToBounds="True" />
                    </VisualBrush.Visual>
                </VisualBrush>
            </Style.Resources>
            <Style.Triggers>
                <Trigger Property="Text" Value="{x:Static sys:String.Empty}">
                    <Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
                </Trigger>
                <Trigger Property="Text" Value="{x:Null}">
                    <Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
                </Trigger>
                <Trigger Property="IsKeyboardFocused" Value="True">
                    <Setter Property="Background" Value="White" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>
mm8
  • 163,881
  • 10
  • 57
  • 88
Piston
  • 95
  • 1
  • 12
  • You should not store this string in xaml, but in a resource file (resx) to have [these advantages](http://stackoverflow.com/a/1134063/5246145) – 3615 Mar 30 '17 at 07:58

1 Answers1

0

You could use a Label Style with a DataTrigger that binds to the window property.

Please refer to the following sample code.

XAML:

<Window x:Class="WpfApplication1.MainWindow"
        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"
        Title="MainWindow" Height="300" Width="300" x:Name="win">
    <StackPanel>
        <TextBox x:Name="txt_reasons" ScrollViewer.VerticalScrollBarVisibility="Auto" HorizontalAlignment="Left" Height="74" TextWrapping="Wrap" VerticalAlignment="Top" Width="344" TabIndex="4" MaxLength="150">
            <TextBox.Style>
                <Style TargetType="TextBox" xmlns:sys="clr-namespace:System;assembly=mscorlib">
                    <Style.Resources>
                        <VisualBrush x:Key="CueBannerBrush" AlignmentX="Left" AlignmentY="Top" Stretch="None">
                            <VisualBrush.Visual>
                                <Label Foreground="Gray" ClipToBounds="True">
                                    <Label.Style>
                                        <Style TargetType="Label">
                                            <Setter Property="Content" Value="Please, write the reason for your request"/>
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding TheLanguage, Source={x:Reference win}}" Value="ES">
                                                    <Setter Property="Content" Value="spanish..."/>
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </Label.Style>
                                </Label>
                            </VisualBrush.Visual>
                        </VisualBrush>
                    </Style.Resources>
                    <Style.Triggers>
                        <Trigger Property="Text" Value="{x:Static sys:String.Empty}">
                            <Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
                        </Trigger>
                        <Trigger Property="Text" Value="{x:Null}">
                            <Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
                        </Trigger>
                        <Trigger Property="IsKeyboardFocused" Value="True">
                            <Setter Property="Background" Value="White" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </TextBox.Style>
        </TextBox>

        <Button Content="Toggle Language" Click="Button_Click" />
    </StackPanel>
</Window>

Code:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private string _language = "EN";
    public string TheLanguage
    {
        get { return _language; }
        set { _language = value; NotifyPropertyChanged(); }
    }


    private void Button_Click(object sender, RoutedEventArgs e)
    {
        TheLanguage = TheLanguage == "EN" ? "ES" : "EN";
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

Note that the window class must implement the INotifyPropertyChanged interface and raise the PropertyChanged event whenever the source property is set to a new value for the Label to get updated.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • Thank you very much @mm8, but I am getting this error: http ://imgur.com/a/PXEP5 Messed with it a lot and can't find whats wrong... – Piston Mar 31 '17 at 10:48
  • Did you really set the x:Name attribute of the window to "win"?: – mm8 Mar 31 '17 at 11:34