无法在XAML中使用Databind派生属性

时间:2010-08-15 14:50:41

标签: c# wpf xaml binding

我在基类(视图模型)中有一个属性,当在XAML中的数据绑定中使用时,它不显示。但是,当我将该属性移动到派生视图模型时,它会显示出来。为什么呢?

XAML:

<TextBlock  Margin="5,0,0,0" VerticalAlignment="Center" 
  Text="{Binding Path=ResxMessages.SelectPathToDevice}" />

代码背后:

public abstract class BaseViewModel{
    protected bool ThrowOnInvalidPropertyName { get; set; }

    /*Putting this property here doesn't work*/
    private static Messages _resxMessages = null;
    public static Messages ResxMessages
    {
      get
      {
        if (_resxMessages == null)
            _resxMessages = new Messages();
        return _resxMessages;
      }
    }
}

public class MainViewModel :BaseViewModel{
    protected bool ThrowOnInvalidPropertyName { get; set; }

    /*Putting this property here DOES work*/
    private static Messages _resxMessages = null;
    public static Messages ResxMessages
    {
      get
      {
        if (_resxMessages == null)
            _resxMessages = new Messages();
        return _resxMessages;
      }
    }
}

1 个答案:

答案 0 :(得分:0)

也许是因为他们被宣布为static(这是你的意图还是偶然)? 检查以下帖子将进一步帮助您:Binding to static property