【问题标题】:INotifyPropertyChanged is NullINotifyPropertyChanged 为空
【发布时间】:2013-05-24 12:15:54
【问题描述】:

INotifyPropertyChanged 在以下 sn-p 中始终为 null

xaml:

<TextBlock
    Name="tbkContent"
    Text="{Binding Path= value,Mode=TwoWay}"
    TextWrapping="Wrap"
    FontSize="22"
    HorizontalAlignment="Center"
    VerticalAlignment="Center"
    TextAlignment="Center"
    Foreground="White"
    Height="100"
    Width="400" />

CS:

// ...
TextContent content = new TextContent();
// ...

public class TextContent:INotifyPropertyChanged
{      

    public event PropertyChangedEventHandler PropertyChanged;       
    protected void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    private string _value;
    public string value
    {
        get
        {
            return _value;
        }
        set
        {
            _value = value;
            this.NotifyPropertyChanged("value");
        }
    }

}

// ...
private void BindingImages(int ImgId)
{
    switch (ImgId)
    {
        case 1:
            content.value = "binding1";
            break;
        case 2:
            content.value = "binding2";
            break;
        case 3:
            content.value = "binding3";
            break;
        default:
            content.value = "binding4";
            _incrID = 0;
            break;
        }
        DoTransitions(ImgBg);
    }

永远不会分配 TextblockValue。这始终为空

if (PropertyChanged != null) // <-----null here
{

}

Textblock 的文本没有正确更新..

最后,我自己动手了,下面的已经成功了

开关(ImgId) {

            case 1:
                _textcontent = new TextContent();
                _textcontent.content = "binding1";
                tbkContent.DataContext = _textcontent;

                break;
            case 2:
                _textcontent = new TextContent();
                _textcontent.content = "binding2";
                tbkContent.DataContext = _textcontent;
                break;
            case 3:
                _textcontent = new TextContent();
                _textcontent.content = "binding3";
                tbkContent.DataContext = _textcontent;
                break;
            default:
                _textcontent = new TextContent();
                _textcontent.content = "binding4";
                tbkContent.DataContext = _textcontent;
                _incrID = 1;
                break;
        }

我错过了设置 datacontext 属性.. 谢谢大家

【问题讨论】:

  • 问题是什么?
  • 括号数量不匹配,代码缩进错误,您帖子的其他部分缺少格式。如果您愿意发布正确的代码并正确使用括号,我很乐意为您编辑。
  • @Jon-- 问题是值没有绑定..
  • 你在哪里设置数据上下文?
  • 不要使用value作为属性名。 value 是 C# 关键字...

标签: silverlight windows-phone-7 mvvm binding inotifypropertychanged


【解决方案1】:

最后,我靠自己了,下面的已经成功了

我错过了设置 datacontext 属性...

switch (ImgId)
{
    case 1:
        _textcontent = new TextContent();
        _textcontent.content = "binding1";
        tbkContent.DataContext = _textcontent;
         break;
    case 2:
        _textcontent = new TextContent();
        _textcontent.content = "binding2";
        tbkContent.DataContext = _textcontent;
        break;
    case 3:
        _textcontent = new TextContent();
        _textcontent.content = "binding3";
        tbkContent.DataContext = _textcontent;
        break;
    default:
        _textcontent = new TextContent();
        _textcontent.content = "binding4";
        tbkContent.DataContext = _textcontent;
        _incrID = 1;
        break;
}

谢谢大家

【讨论】:

  • 就像@ErnodeWeerd 在 cmets 中所说的那样;)
猜你喜欢
  • 2017-08-06
  • 1970-01-01
  • 2011-06-30
  • 2020-05-11
  • 1970-01-01
  • 1970-01-01
  • 2011-01-06
  • 2013-06-07
相关资源
最近更新 更多