簡體   English   中英

使用ItemTemplate的WPFLocalizationExtension

[英]WPFLocalizationExtension with ItemTemplate

我正在為WPF應用程序使用WPFLocalizationExtension 我有一個ComboBox用於語言選擇。 項目源是一個ObservableCollection<KeyValuePair<string, string>> ,如下所示:

  • TITLE_LANGUAGE_ENGLISHen
  • TITLE_LANGUAGE_VIETNAMESEvi-VN

這是我的xaml代碼:

<TextBlock Text="{lex:Loc TITLE_LANGUAGE}"></TextBlock>
<ComboBox Grid.Column="1" 
          ItemsSource="{Binding AvailableLanguages}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{lex:Loc Key={Binding Key}}"></TextBlock>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

當我運行該應用程序時,它拋出如下異常:

不能在“ LocExtension”類型的“ Key”屬性上設置“ Binding”。 只能在DependencyObject的DependencyProperty上設置“綁定”

如何翻譯ItemTemplate

謝謝,

您可以將IMultiValueConverterMultiBinding一起使用,以免失去即時更新本地化的能力。

<ComboBox ItemsSource="{Binding AvailableLanguages}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock>
                <TextBlock.Text>
                    <MultiBinding>
                        <MultiBinding.Bindings>
                            <Binding Path="Key" Mode="OneTime"/>
                            <Binding Path="Culture" Source="{x:Static lex:LocalizeDictionary.Instance}"/>
                        </MultiBinding.Bindings>
                        <MultiBinding.Converter>
                            <l:TranslateMultiConverter/>
                        </MultiBinding.Converter>
                    </MultiBinding>
                </TextBlock.Text>
            </TextBlock>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

這是轉換器:

class TranslateMultiConverter : DependencyObject, IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values.Length == 2)
        {
            var key = values[0] as string;
            if (key == null)
            {                
                return DependencyProperty.UnsetValue;
            }

            var cultureInfo = (values[1] as CultureInfo) ?? culture;

            return LocalizeDictionary.Instance.GetLocalizedObject(key, this, cultureInfo);
        }

        return values.FirstOrDefault();
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

當應用程序的語言發生更改導致MultiBinding刷新值時, LocalizeDictionary將引發PropertyChanged事件。

請注意,轉換器也是DependencyObject 這是為了在調用GetLocalizedObject方法時為LocalizeDictionary提供上下文。

您必須直接綁定到路徑Key DataTemplateTextBlock直接指向單個KeyValuePair對象,您可以直接訪問屬性Key

<TextBlock Text="{lex:Loc TITLE_LANGUAGE}"></TextBlock>
    <ComboBox Grid.Column="1" 
      ItemsSource="{Binding AvailableLanguages}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=Key}" />
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

更新:

也許您必須添加一個Converter。 嘗試WPFLocalizeExtension.TypeConverters.DefaultConverter或自己實現一個從IValueConverter派生的類。

<ComboBox.Resources>
    <cv:DefaultConverter x:Key="DConv" />
</ComboBox.Resources>
<ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=Key, Converter={StaticResource DConv}}" />
        </DataTemplate>
    </ComboBox.ItemTemplate>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM