静态属性的绑定更新 [英] Update binding of static property

查看:162
本文介绍了静态属性的绑定更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该应用程序是用C#和WPF开发的。
我有一个数据绑定到非静态类的静态属性。
当应用程序启动时,结合做得很好,但如果我更改绑定的布尔,视图不会被更新。
我怎样才能更新该静态属性的绑定?
NotifyChanged事件也不会受到影响。

类:

 公共类ViewTemplateManager:NotifyBase
{
    公共静态布尔CanResizeColumns {搞定;组; }    静态ViewTemplateManager()
    {
        CanResizeColumns = TRUE;
    }

视图:

 <拇指×:NAME =PART_HeaderGripperIsEnabled ={绑定源= {X:静态成员= viewManager:ViewTemplateManager.CanResizeColumns}}


解决方案

要做到这一点的唯一方法是,如果你有一个参考相关的<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.data.bindingex$p$pssion.aspx\">BindingEx$p$pssion.

假设你已经在你的code拇指的引用,它看起来像:

  VAR bindingEx pression = thumb.GetBindingEx pression(Thumb.IsEnabledProperty);
如果(bindingEx pression!= NULL)
    bindingEx pression.UpdateTarget();

您最好的选择是使用Singleton模式,像这样:

 公共类ViewTemplateManager:NotifyBase
{
    公共BOOL CanResizeColumns {搞定;组; }    公共静态ViewTemplateManager实例{搞定;私人集; }    静态ViewTemplateManager()
    {
        实例=新ViewTemplateManager();
    }    私人ViewTemplateManager()
    {
        CanResizeColumns = TRUE;
    }
}

然后绑定像这样:

 &LT;拇指×:NAME =PART_HeaderGripperIsEnabled ={绑定源= {X:静态viewManager:ViewTemplateManager.Instance},路径= CanResizeColumns}}

然后你只需要当你改变CanResizeColumns提高INotifyPropertyChanged.PropertyChanged事件。

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 does 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.

这篇关于静态属性的绑定更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆