zv3rk4
zv3rk4

Reputation: 73

WPF: Is there any way to force converter to update if its property gets changed?

Intro: Here is a part of my Translator that I use in my app. I want to update all the strings in it when I change the language with a ComboBox.

Problem: I would like to update labels Content when my Converters Property gets changed. Is it possible? This way (how I made it) doesn't update Content if I change CurrentLanguage.

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

This ComboBox changes my Current value - works

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

Translator code behind - works (PropertyChanged gets fired)

    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
    }

Upvotes: 6

Views: 7424

Answers (2)

zv3rk4
zv3rk4

Reputation: 73

Solution: Use MultiBinding + IMultiValueConverter

ComboBox remained the same.

Edited Laber to use 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>

Changed Translator to 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;
        }

Thx a lot guys!

Upvotes: 1

Philip W
Philip W

Reputation: 791

I see 2 possible solutions:

  • Use a multivalueconverter and bind to Name AND CurrentLanguage
  • More like a hack: Add an eventtrigger to comboxbox.itemchanged and reset the value of Company.Name with Company.Name (setter gets called -> converter gets called)

Upvotes: 3

Related Questions