绑定到静态单例属性时,PropertyChanged为null

时间:2014-11-24 17:51:25

标签: wpf mvvm

<Grid DataContext="{StaticResource BaseViewModel}">
        <DockPanel>
            <TabContro>
                <TabItem DataContext="{StaticResource MainWindowsViewModel}">
                    <Tabs:Main />
                </TabItem>
            </TabControl>
        </DockPanel>
        <Popup IsOpen="{Binding Path=Common.IsLoading}">
            <Control Style="{StaticResource BusyAnimationStyle}"/>
        </Popup>
</Grid>

public class Common : NotificationObject
{
    private static Common _instance = null;

    private bool _isLoading;
    public bool IsLoading
    {
        get { return _isLoading; }
        set
        {
            if (_isLoading != value)
            {
                _isLoading = value;
                RaisePropertyChanged(() => IsLoading);
            }
        }
    }

    protected Common()
    {

    }

    public static Common GetInstance()
    {
        if (_instance == null)
            _instance = new Common();

        return _instance;
    }
}

有2个ViewModel BaseViewModel MainWindowsViewModel

BaseViewModel是MainWindow的父亲 和BaseViewModel具有公共属性

问题是当MainWindowVM更改IsLoading属性时它不起作用 PropertyChanged为null 任何解决方案?

1 个答案:

答案 0 :(得分:0)

您尝试从Common类绑定到IsLoading,但您的绑定意味着IsLoading是静态的,但它不是。

你可能应该有这样的绑定:

{Binding Source={x:Static Common.Instance}, Path=IsLoading}

您还应该使用属性来访问您的单身人士:

public static Common Instance {
    get {return _instance??(_instance = new Common());}
}