C# InotifyProperty更改不起作用

C# InotifyProperty更改不起作用,c#,C#,我正在使用INotifyPropertyChanged,但当我更改属性时,它会给我null,以便我能做什么。。 我的代码是这样的 public class Entities : INotifyPropertyChanged { public Entities(int iCount) { _iCounter = iCount; } private int _iCounter; public int iCounter {

我正在使用INotifyPropertyChanged,但当我更改属性时,它会给我null,以便我能做什么。。 我的代码是这样的

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

谢谢…

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

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

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

这是我认为InotifyProperty中的一个错误已更改

可以有两种变通方法

第一个解决方案

1-将iCounter属性分配给类似Lable的UI控件

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

第二个解决方案

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

我给出了WPF中的演示代码

<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>
最后更改值

 private void Button_Click(object sender, RoutedEventArgs e)
    {
        DemoCustomer dc = this.DataContext as DemoCustomer;

        dc.CompanyName = "Temp";


    }

“当我改变财产时”。这是什么意思?什么给你零?我在这段代码中看到的唯一可以为null的东西是
PropertyChanged
iCounter
是一个
int
值,因此不能为空。如果
PropertyChanged
为null,则表示没有人绑定到它,因此没有人在侦听。哎哟。在iCounter的属性设置器中,您需要的是
\u iCounter=value
值=\u i计数器。你怎么没有注册这个活动?@Martinho:抓得好。这正是为什么我希望看到MS推出某种属性/AOP风格的编程来处理INotifyPropertyChanged,所以我永远不必调试:)这不是INotifyPropertyChanged接口的有效使用为什么这不是INotifyPropertyChanged接口的有效使用。这在ObservaleCollection中经常使用,在ObservaleCollection中,当数据源发生更改时,订阅控件将得到更新,您可以在其中为任何控件设置DataContext。来自MSDN—“INotifyPropertyChanged接口用于通知客户端(通常是绑定客户端)属性值已更改”。我看不出你的回答中使用的有什么不同。
 private void Button_Click(object sender, RoutedEventArgs e)
    {
        DemoCustomer dc = this.DataContext as DemoCustomer;

        dc.CompanyName = "Temp";


    }