Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello!

I try to bind my UI to custom DependencyProperty:
C#
...

<window.resources>
    <local:localization x:key="Localization" xmlns:x="#unknown" xmlns:local="#unknown"></local:localization>
</window.resources>
    <grid name="mainStack" datacontext="{StaticResource Localization}">
         <Button Padding="10,3" Margin="5" Content="{Binding Path=BtnAdd}"   Command="New"/>
    </grid>


Also I have class "Localization":
C#
class Localization : DependencyObject, INotifyPropertyChanged
    {
        public static DependencyProperty BtnAddProperty;
        
        static Localization()
        {
            BtnAddProperty = DependencyProperty.Register("BtnAdd", typeof(string), typeof(Localization));
            
        }

        public string BtnAdd
        {
            set
            {
                SetValue(BtnAddProperty, value);
            }
            get
            {
                return (string)GetValue(BtnAddProperty);
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                PropertyChangedEventArgs e = new PropertyChangedEventArgs(propertyName);
                handler.Invoke(this, e);
            }
        }

        public Localization()
        {
            BtnAdd = MainWindowRes.BtnAdd;
        }

        public void SwitchLanguage()
        {
            BtnAdd = MainWindowRes.BtnAdd;
            OnPropertyChanged("BtnAdd");
        }
       
    }


First time my UI element gets my property value. But when I use my method SwitchLanguage(), property gets new data, and UI still have first value.

Can someone help me please?

P.S.
Sorry, for my English.

Eugene
Posted
Updated 4-Aug-11 22:51pm
v2
Comments
Herman<T>.Instance 5-Aug-11 4:47am    
have you done a debug?
Member 7826605 5-Aug-11 4:51am    
Yes, When calling OnPropertyChanged(), handler PropertyChanged is null
Herman<T>.Instance 5-Aug-11 4:58am    
what if you match to the documentation: http://msdn.microsoft.com/en-us/library/5z57dxz2.aspx ?
Member 7826605 5-Aug-11 5:16am    
Thank you, but it did not help.
Sir.Ixildore 5-Aug-11 14:36pm    
Can you supply a third parameter(FrameworkPropertyMetadata object) to your call to Register and supply it a PropertyChangedCallback object?

1 solution

 BtnAddProperty = DependencyProperty.Register("BtnAdd", typeof(string), typeof(Localization),new FrameworkPropertyMetadata(new PropertyChangedCallback(OnBtnAddPropertyChanged)));
...
private static void OnBtnAddPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
Button btn = o as Button;
... //some localization on button content
btn.OnPropertyChanged(e.Property.Name);
...
}


hope this works.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900