กล่องข้อความว่างบน mousclick ร่วมกับการเปลี่ยนภาษาใน WPF

ฉันมีกล่องข้อความพร้อมข้อความเริ่มต้น เมื่อฉันโฟกัสกล่องข้อความ กล่องข้อความจะถูกล้างเพื่อให้ฉันเขียนได้ และหากฉันเลิกโฟกัสโดยไม่เขียนอะไรเลย ข้อความเริ่มต้นก็จะปรากฏขึ้นอีกครั้ง

ฉันยังมีปุ่มตัวเลือกสองปุ่มสำหรับเลือกภาษา ภาษามีให้เป็นไฟล์ทรัพยากร xaml และข้อความเริ่มต้นในกล่องข้อความเชื่อมต่อกับภาษานั้นโดยใช้ DynamicResource

ปัญหาของฉันคือการเปลี่ยนภาษาใช้งานได้ตราบใดที่ฉันไม่ได้เน้นไปที่กล่องข้อความเท่านั้น หากฉันโฟกัสกล่องข้อความแล้วยกเลิกการโฟกัสโดยไม่เปลี่ยนแปลงสิ่งใด กล่องข้อความจะไม่เปลี่ยนภาษาอีกต่อไป

ฉันเดาว่าเป็นเพราะเมื่อมีการเปลี่ยนแปลง (เคลียร์) จะไม่เชื่อมโยงกับทรัพยากรไดนามิกอีกต่อไป เนื่องจาก WPF ถือว่าการเปลี่ยนแปลง onfocus ของฉันเป็นอินพุตของผู้ใช้ แต่ฉันไม่สามารถทราบวิธีแก้ไขและทำให้มันเปลี่ยนภาษาด้วยซ้ำ ถ้าฉันคลิกกล่องข้อความ

กล่องข้อความที่สองไม่มีพฤติกรรมการโฟกัส และในกล่องข้อความนั้น การเปลี่ยนภาษาทำงานได้ตามที่ควรจะเป็น กล่าวคือ เปลี่ยนภาษาตราบใดที่ฉันไม่ได้เขียนอะไรบางอย่างจริงๆ

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

Windows หลัก 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);
    }
}
}

ภาษาอังกฤษ 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>

ภาษา 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>

person Stefan    schedule 11.07.2016    source แหล่งที่มา


คำตอบ (1)


ใช่ เมื่อคุณตั้งค่า TextBox.Text ใน codebehind ข้อความจะไม่ทราบอีกต่อไปว่าจะต้องรับค่าจาก Rersource เพื่อหลีกเลี่ยงปัญหานี้ คุณสามารถเปลี่ยนข้อความโดยใช้ XAML ล้วนๆ พร้อมทริกเกอร์ได้

ลบตัวจัดการเหตุการณ์ของกล่องข้อความและเพิ่มสไตล์ดังนี้:

    <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>
person lena    schedule 13.01.2017