【发布时间】:2017-03-30 07:50:43
【问题描述】:
我有这个 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>
【问题讨论】:
-
您不应将此字符串存储在 xaml 中,而应将其存储在资源文件 (resx) 中以具有 these advantages