简体   繁体   English

文本框占位符翻译WPF XAML

[英]Textbox placeholder translations WPF XAML

I have this XAML code that creates a txtbox with a placeholder text, that appears and disapears if it's focused or not. 我有这个XAML代码,它创建一个带有占位符文本的txtbox,如果它聚焦与否则会出现并消失。

But right now, I need to translate the aplication to 2 languages with a button. 但是现在,我需要用一个按钮将应用程序翻译成2种语言。 That button simply sets a global variable to "EN" for english and "ES" for spanish. 该按钮只是将全局变量设置为英语的“EN”和西班牙语的“ES”。

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>

You could use a Label Style with a DataTrigger that binds to the window property. 您可以将Label StyleDataTrigger一起使用,该DataTrigger绑定到window属性。

Please refer to the following sample code. 请参阅以下示例代码。

XAML: 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. 请注意,只要将source属性设置为Label的新值以进行更新,窗口类就必须实现INotifyPropertyChanged接口并引发PropertyChanged事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM