WPF绑定在Windows XP中不起作用

时间:2015-03-11 15:12:56

标签: c# wpf windows binding windows-xp

我有以下课程:

public abstract class TestBase<T> : INotifyPropertyChanged where T : class
{
    private static T _instance;
    public event PropertyChangedEventHandler PropertyChanged;

    public static T Instance
    {
        get
        {
            lock (Lock)
            {
                var instance = _instance ?? (Activator.CreateInstance<T>());

                _instance = instance;
                return instance;
            }
        }
    }

    // ReSharper disable once StaticMemberInGenericType
    public static object Lock = new object();

    protected virtual void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
}


public class Test : TestBase<Test>
{
    private bool _isDone = true;

    public bool IsDone
    {
        get
        {
            return _isDone;
        }
        set
        {
            _isDone = value;
        }
    }
}

然后,我在xaml声明了这个:

<lib:Test x:Key="Test" />

并像这样使用:

IsChecked="{Binding Source={StaticResource Test}, Path=Instance.IsDone}"

这在Vista及更高版本上运行良好,但不在XP上,它只是没有绑定(回退到默认值)。我们希望我们可以放弃XP,但事实并非如此。我试图通过Snoop找到更多信息,但它在我们所有的XP机器上都崩溃了。我在这里拍摄空白并且没有想法,所以我很好奇这里是否有人对此问题有任何想法。

作为一种解决方法,我可以在Test类中添加类似的内容:

public static Test InstanceXP 
{
   get 
   {
       return Instance;
   }
}

但这是一个糟糕的解决方法。

1 个答案:

答案 0 :(得分:0)

我神奇地偶然发现了另一种适用于Windows XP的解决方法。

不是在&lt; ... Resources&gt;中声明它,而是使用以下表示法:

 {Binding Source={x:Static lib:Test.Instance}, Path=IsDone}

这适用于所有Windows版本。

相关问题