文本框占位符翻译WPF XAML

时间:2017-03-30 07:50:43

标签: c# wpf xaml

我有这个XAML代码,它创建一个带有占位符文本的txtbox,如果它是聚焦的话,它会出现并消失。

但是现在,我需要用一个按钮将应用程序翻译成2种语言。该按钮只是将全局变量设置为英语的“EN”和西班牙语的“ES”。

我如何调整代码,因此根据该变量(用后面的代码编写)文本“请,请写下您的请求的原因”更改?

这是现在正在运行的代码:

<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>

1 个答案:

答案 0 :(得分:0)

您可以使用Label StyleDataTrigger绑定到窗口属性。

请参阅以下示例代码。

<强> 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>

<强>代码:

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));
    }
}

请注意,只要将source属性设置为PropertyChanged的新值,窗口类就必须实现INotifyPropertyChanged接口并引发Label事件。