WPF:如果其属性发生变化,有没有办法强制转换器更新?

问题描述 投票:2回答:2

简介:这是我在我的应用程序中使用的翻译器的一部分。当我用ComboBox更改语言时,我想更新其中的所有字符串。

问题:当我的转换器属性发生变化时,我想更新标签内容。可能吗?如果我更改CurrentLanguage,这种方式(我如何制作)不会更新内容。

 <Label
      ID:Name="CompanyName"
      Content="{Binding ElementName=CompanyName, Path=Name, Converter={ID:Static Controller:Translator.Instance}}" />

此ComboBox更改我的当前值 - 工作

 <ComboBox
        SelectedItem="{Binding Path=CurrentLanguage, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, Converter={StaticResource FlagConverter}}">

翻译代码背后 - 工作(PropertyChanged被解雇)

    public partial class Translator : IValueConverter, INotifyPropertyChanged
    { 
    ...
        private String m_currentLanguage;
        public String CurrentLanguage
        {
            get { return m_currentLanguage; }
            set
            {
                m_currentLanguage = value;
                OnPropertyChanged("CurrentLanguage");
            }
        }

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return Get((String)value); // nonrelevant function - works
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return GetOriginal((String)value); // nonrelevant function - works
        }

        #region Events
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion
    }
c# wpf xaml inotifypropertychanged ivalueconverter
2个回答
2
投票

我看到两种可能的解决方案

  • 使用多值转换器并绑定到Name AND CurrentLanguage
  • 更像是一个hack:将一个eventtrigger添加到comboxbox.itemchanged并使用Company.Name重置Company.Name的值(setter被调用 - >转换器被调用)

1
投票

解决方案:使用MultiBinding + IMultiValueConverter

ComboBox保持不变。

编辑Laber使用MultiBinding。

                <Label
                    ID:Name="CompanyName"
                    <Label.Content>
                        <MultiBinding Converter="{ID:Static Controller:Translator.Instance}">
                            <Binding ElementName="CompanyName" Path="Name"/>
                            <Binding Source="{ID:Static Controller:Translator.Instance}" Path="CurrentLanguage"/>
                        </MultiBinding>
                    </Label.Content>
                </Label>

将Translator更改为IMultiValueConverter:

        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if ((values[0] as String).Length <= 0)
                return ""; // prevents error messages for binds on element names

            return Get((String)values[0]);
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }

很多人!

© www.soinside.com 2019 - 2024. All rights reserved.