INotifyPropertyChanged is Null



INotifyPropertyChanged在以下代码段中始终为空

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
{
}

最后,我自己搞定了,下面的代码成功了

开关(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;
        }

我错过了设置数据上下文属性…谢谢大家

最后,我自己搞定了,下面的方法已经成功了

我错过了设置数据上下文属性…

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;
}

谢谢大家

相关内容

  • 没有找到相关文章

最新更新