-2

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

xml:

<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 属性.. 谢谢大家

4

1 回答 1

0

最后,我自己上了,下面已经完成了

我错过了设置 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;
}

谢谢你们

于 2013-05-29T11:57:03.267 回答