首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >INotifyPropertyChanged不工作

INotifyPropertyChanged不工作
EN

Stack Overflow用户
提问于 2010-12-27 15:26:01
回答 2查看 4.3K关注 0票数 1

我正在使用INotifyPropertyChanged,但是当我对PropertyChanged进行shaw时,它会给我null,所以我可以做什么..我的代码是这样的。

代码语言:javascript
复制
public class Entities : INotifyPropertyChanged
{

    public Entities(int iCount)
    {
        _iCounter = iCount;
    }


    private int _iCounter;
    public int iCounter
    {
        get
        {
            return _iCounter;
        }
        set
        {
            value = _iCounter;
            NotifyPropertyChanged("iCounter");
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion

    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}

谢谢..。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-12-27 16:56:12

我认为这是INotifyPropertyChanged中的一个bug。

可以有两种解决方法

第一个解决方法

1-将iCounter属性分配给像Lable这样的UI控件。

2-现在更改属性值,PropertyChanged事件将引用您的方法,并且不会为空;

第2个解决方法

在Entities类构造函数中分配PropertyChanged委托

我给出了WPF的演示代码

代码语言:javascript
复制
<Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <Grid.Resources>
            <ToolTip x:Key="@tooltip">
                <TextBlock Text="{Binding CompanyName}"/>
        </ToolTip>
        </Grid.Resources>

        <TextBlock Text="{Binding Name}" Background="LightCoral" />
    <Rectangle Width="200" Height="200" Fill="LightBlue" VerticalAlignment="Center" HorizontalAlignment="Center" ToolTip="{DynamicResource @tooltip}" Grid.Row="1"/>
    <Button Click="Button_Click" Grid.Row="2" Margin="20">Click Me</Button>
</Grid>

请看这里,CompanyName被分配给一个工具提示。

//这是Window1.Cs文件公共Window1() { DataContext = DemoCustomer.CreateNewCustomer();InitializeComponent();}

//现在DemoCustomer类

代码语言:javascript
复制
    public class DemoCustomer : INotifyPropertyChanged
{
    // These fields hold the values for the public properties.
    private Guid idValue = Guid.NewGuid();
    private string customerName = String.Empty;
    private string companyNameValue = String.Empty;
    private string phoneNumberValue = String.Empty;

    public event PropertyChangedEventHandler PropertyChanged;



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

    // The constructor is private to enforce the factory pattern.
    private DemoCustomer()
    {
        customerName = "no data";
        companyNameValue = "no data";
        phoneNumberValue = "no data";
    }

    // This is the public factory method.
    public static DemoCustomer CreateNewCustomer()
    {
        return new DemoCustomer();
    }

    // This property represents an ID, suitable
    // for use as a primary key in a database.
    public Guid ID
    {
        get
        {
            return this.idValue;
        }
    }

    public string CompanyName
    {
        get { return this.companyNameValue; }

        set
        {
            if (value != this.companyNameValue)
            {
                this.companyNameValue = value;
                OnPropertyChanged("CompanyName");
            }
        }
    }
    public string PhoneNumber
    {
        get { return this.phoneNumberValue; }

        set
        {
            if (value != this.phoneNumberValue)
            {
                this.phoneNumberValue = value;
                OnPropertyChanged("PhoneNumber");
            }
        }
    }
}

最后更改该值

代码语言:javascript
复制
 private void Button_Click(object sender, RoutedEventArgs e)
    {
        DemoCustomer dc = this.DataContext as DemoCustomer;

        dc.CompanyName = "Temp";


    }
票数 -1
EN

Stack Overflow用户

发布于 2010-12-27 15:34:31

我试着把你的代码放到我的程序中,它工作得很好。我将获取EventArg作为属性:

代码语言:javascript
复制
class Program
    {
        static void Main(string[] args)
        {
            var ent = new Entities(10);
            ent.PropertyChanged += new PropertyChangedEventHandler(ent_PropertyChanged);
            ent.iCounter = 100;
        }

        static void ent_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            throw new NotImplementedException();
        }
    }


    public class Entities : INotifyPropertyChanged
    {

        public Entities(int iCount)
        {
            _iCounter = iCount;
        }
        private int _iCounter;
        public int iCounter
        {
            get
            {
                return _iCounter;
            }
            set
            {
                _iCounter = value;
                NotifyPropertyChanged("iCounter");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
    }

你得到的确切的错误是什么?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4537205

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档