根据属性值显示另一个动态资源

时间:2021-07-04 23:53:42

within a textblock i would like a text, depending on the value of a boolean property. the text has to be taken using a DynamicResource, because of the multiple langauges the application supports.

在文本块中我想要一个文本,具体取决于布尔属性的值。由于应用程序支持多种语言,因此必须使用DynamicResource来获取文本。

there are two entries available in the DynamicResources, one for when the property is true, one for when the property is false.

DynamicResources中有两个条目,一个用于属性为true,另一个用于属性为false时。

i would like to solve this in xaml, so that the correct DynamicResource is shown, depending on the value of the property.

我想在xaml中解决这个问题,以便显示正确的DynamicResource,具体取决于属性的值。

anybody an idea or this is possible ? (and how ;-) ? thanks.

任何人有想法或这是可能的吗? (如何 ;-) ?谢谢。

1 个解决方案

#1


0  

Below is solution for your problem.

以下是您的问题的解决方案。

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

  • MainWindow.xaml                                                TextBlock的> 网格>

  • 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));
            }
        }
    }
}

Better option is create two language files with same keys but with different values and merged proper files on runtime. In this case you don't have to check boolean property every time!

更好的选择是创建两个具有相同键但具有不同值的语言文件,并在运行时合并适当的文件。在这种情况下,您不必每次都检查布尔属性!

#1


0  

Below is solution for your problem.

以下是您的问题的解决方案。

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

  • MainWindow.xaml                                                TextBlock的> 网格>

  • 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));
            }
        }
    }
}

Better option is create two language files with same keys but with different values and merged proper files on runtime. In this case you don't have to check boolean property every time!

更好的选择是创建两个具有相同键但具有不同值的语言文件,并在运行时合并适当的文件。在这种情况下,您不必每次都检查布尔属性!