1

テキストブロック内で、ブールプロパティの値に応じてテキストが必要です。アプリケーションがサポートする複数の言語のため、テキストはDynamicResourceを使用して取得する必要があります。

DynamicResourcesには2つのエントリがあります。1つはプロパティがtrueの場合、もう1つはプロパティがfalseの場合です。

これをxamlで解決して、プロパティの値に応じて正しいDynamicResourceが表示されるようにします。

誰かアイデアやこれは可能ですか?(そしてどうやって ;-) ?ありがとう。

4

1 に答える 1

0

以下はあなたの問題の解決策です。

  • 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="Lang1">Lang1 Message</system:String>
    <system:String x:Key="Lang2">Lang2 Message</system:String>
</ResourceDictionary>
  • App.xaml
<Application x:Class="MultiLang.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="LangResources.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>
  • MainWindow.xaml

    <Window.Resources>
        <vm:MainWindowViewModel x:Key="mainVM" />
    </Window.Resources>
    
    <Grid DataContext="{StaticResource mainVM}">
    
        <ToggleButton Name="btnLang" IsChecked="{Binding Var}" Content="Change lang" MaxWidth="100" MaxHeight="30">
    
        </ToggleButton>
    
        <TextBlock Margin="5" MaxHeight="30" VerticalAlignment="Top">
            <TextBlock.Style>
                <Style TargetType="{x:Type TextBlock}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=DataContext.Var, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Grid}}" Value="True">
                            <Setter Property="Text" Value="{DynamicResource Lang1}" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Path=DataContext.Var, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Grid}}" Value="False">
                            <Setter Property="Text" Value="{DynamicResource Lang2}" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock> 
    </Grid>
    

  • MainWindowViewModel

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace MultiLang
{
    public class MainWindowViewModel: INotifyPropertyChanged
    {
        private bool _var = false;
        public bool Var
        {
            get { return _var; }
            set
            {
                _var = value;
                OnPropertyChanged("Var");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string name)
        {        
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {

                handler(this, new PropertyChangedEventArgs(name));
            }
        }
    }
}

より良いオプションは、同じキーで異なる値を持つ2つの言語ファイルを作成し、実行時に適切なファイルをマージすることです。この場合、ブールプロパティを毎回チェックする必要はありません。

于 2012-05-24T21:31:56.907 に答える