【问题标题】:Empty textbox on mousclick in combination with language change in WPFmousclick 上的空文本框与 WPF 中的语言更改相结合
【发布时间】:2016-11-13 03:55:36
【问题描述】:

我有一个带有默认文本的文本框。当我聚焦文本框时,它会被清除,以便我可以写,如果我不写任何东西就取消聚焦,默认文本会重新出现。

我还有两个用于选择语言的单选按钮。语言以 xaml 资源文件的形式提供,文本框中的默认文本使用 DynamicResource 连接到该文本。

我的问题是,只要我没有关注文本框,语言更改才会起作用。如果我聚焦文本框,然后在不更改任何内容的情况下取消聚焦,则文本框不再更改语言。

我猜这是因为一旦更改(清除)它就不再链接到动态资源,因为 WPF 将我的 onfocus 更改视为用户输入,但我不知道如何解决这个问题并使它即使我点击了文本框也可以更改语言。

第二个文本框没有任何焦点行为,并且在该文本框中,语言更改按应有的方式进行,即只要我还没有真正写过东西,它就会更改语言。

主窗口 xaml:

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

MainWindows cs:

using System;
using System.Windows;
using System.Windows.Controls;

namespace Textbox_langauge_buggseek
{
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        SetLanguageDictionary();
    }

    //*****************************************************************************************

    private void TextBox_GotFocus(object sender, RoutedEventArgs e)
    {
        TextBox box = sender as TextBox;
        box.Text = box.Text == (string)this.Resources["TB"] ? string.Empty : box.Text;
    }

    private void TextBox_LostFocus(object sender, RoutedEventArgs e)
    {
        TextBox box = sender as TextBox;
        box.Text = box.Text == string.Empty ? (string)this.Resources["TB"] : box.Text;
    }

    //*****************************************************************************************

    private void En_Checked(object sender, RoutedEventArgs e)
    {
        SetLanguageDictionary("En");
    }

    private void Se_Checked(object sender, RoutedEventArgs e)
    {
        SetLanguageDictionary("Se");
    }

    //*****************************************************************************************

    private void SetLanguageDictionary(string language = "En")
    {
        ResourceDictionary dict = new ResourceDictionary();
        switch (language)
        {
            case "Se":
                dict.Source = new Uri("..\\Resources\\Se.xaml", UriKind.Relative);
                break;
            default:
                dict.Source = new Uri("..\\Resources\\En.xaml", UriKind.Relative);
                break;
        }
        this.Resources.MergedDictionaries.Add(dict);
    }
}
}

En 语言 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>

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 i textrutan!</system:String>

</ResourceDictionary>

【问题讨论】:

    标签: wpf textbox focus


    【解决方案1】:

    是的,当您在代码隐藏中设置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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多