Binding to static property

后端 未结 12 2141
夕颜
夕颜 2020-11-22 05:14

I\'m having a hard time binding a simple static string property to a TextBox.

Here\'s the class with the static property:



        
12条回答
  •  孤独总比滥情好
    2020-11-22 05:36

    You can't bind to a static like that. There's no way for the binding infrastructure to get notified of updates since there's no DependencyObject (or object instance that implement INotifyPropertyChanged) involved.

    If that value doesn't change, just ditch the binding and use x:Static directly inside the Text property. Define app below to be the namespace (and assembly) location of the VersionManager class.

    
    

    If the value does change, I'd suggest creating a singleton to contain the value and bind to that.

    An example of the singleton:

    public class VersionManager : DependencyObject {
        public static readonly DependencyProperty FilterStringProperty =
            DependencyProperty.Register( "FilterString", typeof( string ),
            typeof( VersionManager ), new UIPropertyMetadata( "no version!" ) );
        public string FilterString {
            get { return (string) GetValue( FilterStringProperty ); }
            set { SetValue( FilterStringProperty, value ); }
        }
    
        public static VersionManager Instance { get; private set; }
    
        static VersionManager() {
            Instance = new VersionManager();
        }
    }
    
    
    

提交回复
热议问题