开发者

Update binding of static property

The application is developed with C# and WPF. I have a data binding to a static property of a non static class. When the application is started, the binding doe开发者_如何学Cs well, but if i change the bool of the binding, the view is not been updated. How can i update the binding of this static property ? NotifyChanged-Events don't affected.

The class:

public class ViewTemplateManager : NotifyBase
{
    public static bool CanResizeColumns { get; set; }

    static ViewTemplateManager()
    {
        CanResizeColumns = true;
    }

The View:

<Thumb x:Name="PART_HeaderGripper" IsEnabled="{Binding Source={x:Static Member=viewManager:ViewTemplateManager.CanResizeColumns}}"


The only way to do this is if you have a reference to the associated BindingExpression.

Assuming you have a reference to the Thumb in your code, it would look like:

var bindingExpression = thumb.GetBindingExpression(Thumb.IsEnabledProperty);
if (bindingExpression != null)
    bindingExpression.UpdateTarget();

Your best bet would be to use a singleton pattern, like so:

public class ViewTemplateManager : NotifyBase
{
    public bool CanResizeColumns { get; set; }

    public static ViewTemplateManager Instance { get; private set; }

    static ViewTemplateManager()
    {
        Instance = new ViewTemplateManager();
    }

    private ViewTemplateManager()
    {
        CanResizeColumns = true;
    }
}

Then bind like so:

<Thumb x:Name="PART_HeaderGripper" IsEnabled="{Binding Source={x:Static viewManager:ViewTemplateManager.Instance}, Path=CanResizeColumns}}"

Then you simply need to raise the INotifyPropertyChanged.PropertyChanged event when you change CanResizeColumns.


Unfortunately, there is no direct equivalent to the notification mechanism of INotifyPropertyChanged for a static property.

There are a couple of options, including:

  1. Make an instance level property that returns the static member. Use a custom mechanism to notify instances of the change, at which time the instance can raise the PropertyChanged event. This can get ugly and introduce memory leaks if you're not careful about unsubscribing or using a Weak Event pattern.
  2. Move the static property into a singleton, and place it as an instance member on the singleton instance. This allows that instance to raise PropertyChanged events as normal.


You can either implement the properties using a Singleton on which the property is not static or you just also make it non-static but you just create one instance in the App instance for example (i would go with the latter). In either case you can implement INotifyPropertyChanged again.


I think it is easier solution:

Class property:

private static bool isEnabled; //there is Your static

public bool IsEnabled
{
    get { return isEnabled; }
    set
    {
        isEnabled = value;
        RaisePropertyChanged("IsEnabled"); //OnPropertyChanged or something
    }
}

XAML:

<Button IsEnabled="{Binding IsEnabled}"/>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜