2016-07-11 2 views
1

私はデフォルトテキストのテキストボックスを持っています。テキストボックスにフォーカスを当てるとクリアされ、書き込むことができます。何も書き込まずにフォーカスを外すと、デフォルトのテキストが再び表示されます。WPFの言語変更と組み合わせたmousclickの空のテキストボックス

また、言語を選択するためのラジオボタンが2つあります。言語はxamlリソースファイルとして提供され、テキストボックスのデフォルトテキストはDynamicResourceを使用するテキストファイルに接続されます。

私の問題は、言語の変更がテキストボックスに焦点を当てていない限り動作することです。テキストボックスにフォーカスを当てて何も変更せずにフォーカスを外すと、テキストボックスは言語を変更しなくなります。

WPFは自分のオンフォーカスの変更をユーザーの入力として考慮しているため、一度変更(クリア)されるとダイナミックリソースにリンクされなくなったためだと思いますが、それを回避して私はテキストボックスをクリックしても言語を変更します。

2番目のテキストボックスにはフォーカスビヘイビアがありません。その場合、言語の変更は必要に応じて機能します。つまり、実際に何かを書いていない限り、言語が変更されます。

メイン・ウィンドウ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); 
    } 
} 
} 

エン言語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> 

答えて

0

はい、コードビハインドでTextBox.Textを設定すると、テキストはRersourceから値を取らなければならないということをもう知りません。これを避けるために、純粋な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> 
関連する問題