Binding to static property

后端 未结 12 2138
夕颜
夕颜 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:30

    You can use ObjectDataProvider class and it's MethodName property. It can look like this:

    <Window.Resources>
       <ObjectDataProvider x:Key="versionManager" ObjectType="{x:Type VersionManager}" MethodName="get_FilterString"></ObjectDataProvider>
    </Window.Resources>
    

    Declared object data provider can be used like this:

    <TextBox Text="{Binding Source={StaticResource versionManager}}" />
    
    0 讨论(0)
  • 2020-11-22 05:30

    Look at my project CalcBinding, which provides to you writing complex expressions in Path property value, including static properties, source properties, Math and other. So, you can write this:

    <TextBox Text="{c:Binding local:VersionManager.FilterString}"/>
    

    Goodluck!

    0 讨论(0)
  • 2020-11-22 05:33

    Another solution is to create a normal class which implements PropertyChanger like this

    public class ViewProps : PropertyChanger
    {
        private string _MyValue = string.Empty;
        public string MyValue
        {
            get { 
                return _MyValue
            }
            set
            {
                if (_MyValue == value)
                {
                    return;
                }
                SetProperty(ref _MyValue, value);
            }
        }
    }
    

    Then create a static instance of the class somewhere you wont

    public class MyClass
    {
        private static ViewProps _ViewProps = null;
        public static ViewProps ViewProps
        {
            get
            {
                if (_ViewProps == null)
                {
                    _ViewProps = new ViewProps();
                }
                return _ViewProps;
            }
        }
    }
    

    And now use it as static property

    <TextBlock  Text="{x:Bind local:MyClass.ViewProps.MyValue, Mode=OneWay}"  />
    

    And here is PropertyChanger implementation if necessary

    public abstract class PropertyChanger : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
        {
            if (object.Equals(storage, value)) return false;
    
            storage = value;
            OnPropertyChanged(propertyName);
            return true;
        }
    
        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    
    0 讨论(0)
  • 2020-11-22 05:34

    If the binding needs to be two-way, you must supply a path.

    There's a trick to do two-way binding on a static property, provided the class is not static : declare a dummy instance of the class in the resources, and use it as the source of the binding.

    <Window.Resources>
        <local:VersionManager x:Key="versionManager"/>
    </Window.Resources>
    ...
    
    <TextBox Text="{Binding Source={StaticResource versionManager}, Path=FilterString}"/>
    
    0 讨论(0)
  • 2020-11-22 05:34

    There could be two ways/syntax to bind a static property. If p is a static property in class MainWindow, then binding for textbox will be:

    1.

    <TextBox Text="{x:Static local:MainWindow.p}" />
    

    2.

    <TextBox Text="{Binding Source={x:Static local:MainWindow.p},Mode=OneTime}" />
    
    0 讨论(0)
  • 2020-11-22 05:34

    Right variant for .NET 4.5 +

    C# code

    public class VersionManager
    {
        private static string filterString;
    
        public static string FilterString
        {
            get => filterString;
            set
            {
                if (filterString == value)
                    return;
    
                filterString = value;
    
                StaticPropertyChanged?.Invoke(null, FilterStringPropertyEventArgs);
            }
        }
    
        private static readonly PropertyChangedEventArgs FilterStringPropertyEventArgs = new PropertyChangedEventArgs (nameof(FilterString));
        public static event PropertyChangedEventHandler StaticPropertyChanged;
    }
    

    XAML binding (attention to braces they are (), not {})

    <TextBox Text="{Binding Path=(yournamespace:VersionManager.FilterString)}" />
    
    0 讨论(0)
提交回复
热议问题