mousclick上的空文本框与WPF中的语言更改相结合
我有一个带有默认文本的文本框。当我聚焦文本框时,它会被清除,以便我可以写入,如果我在不写入任何内容的情况下取消聚焦,默认文本会重新出现 我还有两个单选按钮用于选择语言。语言作为xaml资源文件提供,文本框中的默认文本使用DynamicSource连接到该文本 我的问题是,只有在我没有关注文本框的情况下,语言变化才会起作用。如果我聚焦文本框,然后在不改变任何内容的情况下取消聚焦,文本框将不再改变语言 我猜这是因为一旦它被更改(清除),它就不再链接到动态资源,因为WPF将我的onfocus更改视为用户输入,但我不知道如何绕过它,并使它更改语言,即使我单击了文本框 第二个文本框没有任何焦点行为,在其中一个文本框中,语言更改按其应该的方式工作,即,只要我没有实际写东西,它就会更改语言 主窗口xaml:mousclick上的空文本框与WPF中的语言更改相结合,wpf,textbox,focus,Wpf,Textbox,Focus,我有一个带有默认文本的文本框。当我聚焦文本框时,它会被清除,以便我可以写入,如果我在不写入任何内容的情况下取消聚焦,默认文本会重新出现 我还有两个单选按钮用于选择语言。语言作为xaml资源文件提供,文本框中的默认文本使用DynamicSource连接到该文本 我的问题是,只有在我没有关注文本框的情况下,语言变化才会起作用。如果我聚焦文本框,然后在不改变任何内容的情况下取消聚焦,文本框将不再改变语言 我猜这是因为一旦它被更改(清除),它就不再链接到动态资源,因为WPF将我的onfocus更改视为用
<Window x:Class="Textbox_langauge_buggseek.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"
xmlns:local="clr-namespace:Textbox_langauge_buggseek"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBox x:Name="TextBox" HorizontalAlignment="Left" Height="46" Margin="84,55,0,0" TextWrapping="Wrap" Text="{DynamicResource ResourceKey=TB}" VerticalAlignment="Top" Width="334" GotFocus="TextBox_GotFocus" LostFocus="TextBox_LostFocus"/>
<TextBox x:Name="TextBox_Copy" HorizontalAlignment="Left" Height="46" Margin="84,123,0,0" TextWrapping="Wrap" Text="{DynamicResource ResourceKey=TB}" VerticalAlignment="Top" Width="334"/>
<RadioButton x:Name="En" Content="En" GroupName="Lang" HorizontalAlignment="Left" Margin="391,216,0,0" VerticalAlignment="Top" Checked="En_Checked" IsChecked="True"/>
<RadioButton x:Name="Se" Content="Se" GroupName="Lang" HorizontalAlignment="Left" Margin="391,234,0,0" VerticalAlignment="Top" Checked="Se_Checked"/>
</Grid>
</Window>
英语xaml:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib">
<system:String x:Key="TB">Text in the TextBox!</system:String>
</ResourceDictionary>
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib">
<system:String x:Key="TB">Text i textrutan!</system:String>
</ResourceDictionary>
文本框中的文本!
Se语言xaml:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib">
<system:String x:Key="TB">Text in the TextBox!</system:String>
</ResourceDictionary>
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib">
<system:String x:Key="TB">Text i textrutan!</system:String>
</ResourceDictionary>
我发短信给鲁坦!
是,当您在codebehind中设置TextBox.Text
时,文本不再知道必须从源中获取值。为了避免这种情况,您可以使用纯XAML和触发器来更改文本
删除TextBox的事件处理程序并添加如下样式:
<TextBox x:Name="TextBox" HorizontalAlignment="Left" Height="46" Margin="84,55,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="334">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="IsFocused" Value="true">
<Setter Property="Text" Value="" />
</Trigger>
<Trigger Property="IsFocused" Value="false">
<Setter Property="Text" Value="{DynamicResource ResourceKey=TB}" />
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
是,当您在codebehind中设置
TextBox.Text
时,文本不再知道必须从源中获取值。为了避免这种情况,您可以使用纯XAML和触发器来更改文本
删除TextBox的事件处理程序并添加如下样式:
<TextBox x:Name="TextBox" HorizontalAlignment="Left" Height="46" Margin="84,55,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="334">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="IsFocused" Value="true">
<Setter Property="Text" Value="" />
</Trigger>
<Trigger Property="IsFocused" Value="false">
<Setter Property="Text" Value="{DynamicResource ResourceKey=TB}" />
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>