C# INotifyPropertyChanged和数据绑定

C# INotifyPropertyChanged和数据绑定,c#,wpf,xaml,C#,Wpf,Xaml,我正在做一个处理读卡器的测试应用程序,我有一个带有读卡器状态的枚举和一个带有TextBlock的XAML窗口,我希望当状态更改onPropertyChanged时,用状态的名称更改TextBlock 以下是我的部分代码: public class CardControler : INotifyPropertyChanged { private CardState state; public CardState State { get { return

我正在做一个处理读卡器的测试应用程序,我有一个带有读卡器状态的枚举和一个带有TextBlock的XAML窗口,我希望当状态更改onPropertyChanged时,用状态的名称更改TextBlock

以下是我的部分代码:

public class CardControler : INotifyPropertyChanged
{

    private CardState state;

    public CardState State
    {
        get { return state; }
        set
        {
            if (state != value)
            {
                state = value;
                OnPropertyChanged(state);
            }
        }
    }
public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(CardState state)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(state.ToString()));
        }

    }

public partial class CardReader : Window
{

    public CardControler control { get; set; }

    public CardReader(int port)
    {

        this.DataContext = control;
        this.port = port;            
        InitializeComponent();
        ScreenWrite(CardState.Initializing);
        Thread thread = new Thread(new ThreadStart(asincControlCreate));
        thread.Start();

    }
在xaml中

<TextBlock Name="Screen" Text="{Binding Path=control.state}></TextBlock>

您应该传递更改的属性的名称,而不是其值:

PropertyChanged(this, new PropertyChangedEventArgs("State"));

您应该传递更改的属性的名称,而不是其值:

PropertyChanged(this, new PropertyChangedEventArgs("State"));

您应该传递更改的属性的名称,而不是其值:

PropertyChanged(this, new PropertyChangedEventArgs("State"));

您应该传递更改的属性的名称,而不是其值:

PropertyChanged(this, new PropertyChangedEventArgs("State"));

以下行不正确,因为您应该将
propertyName
作为参数传递,而不是
state.ToString()

因此,您的代码应该如下所示:

public CardState State
{
    get { return state; }
    set
    {
        if (state != value)
        {
            state = value;
            OnPropertyChanged("State");
        }
    }
}

public event PropertyChangedEventHandler PropertyChanged;

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

除此之外,请记住
xaml
是区分大小写的,因此
{Binding State}
{Binding State}
不同,下面的行是不正确的,因为您应该将
propertyName
作为参数传递,而不是
State.ToString()

因此,您的代码应该如下所示:

public CardState State
{
    get { return state; }
    set
    {
        if (state != value)
        {
            state = value;
            OnPropertyChanged("State");
        }
    }
}

public event PropertyChangedEventHandler PropertyChanged;

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

除此之外,请记住
xaml
是区分大小写的,因此
{Binding State}
{Binding State}
不同,下面的行是不正确的,因为您应该将
propertyName
作为参数传递,而不是
State.ToString()

因此,您的代码应该如下所示:

public CardState State
{
    get { return state; }
    set
    {
        if (state != value)
        {
            state = value;
            OnPropertyChanged("State");
        }
    }
}

public event PropertyChangedEventHandler PropertyChanged;

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

除此之外,请记住
xaml
是区分大小写的,因此
{Binding State}
{Binding State}
不同,下面的行是不正确的,因为您应该将
propertyName
作为参数传递,而不是
State.ToString()

因此,您的代码应该如下所示:

public CardState State
{
    get { return state; }
    set
    {
        if (state != value)
        {
            state = value;
            OnPropertyChanged("State");
        }
    }
}

public event PropertyChangedEventHandler PropertyChanged;

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

除此之外,请记住,
xaml
是区分大小写的,因此
{Binding State}
{Binding State}
不同,绑定中的属性大小写需要与公共属性匹配(
State
,而不是
State
):


绑定中属性的大小写需要与公共属性匹配(
状态
,而不是
状态
):


绑定中属性的大小写需要与公共属性匹配(
状态
,而不是
状态
):


绑定中属性的大小写需要与公共属性匹配(
状态
,而不是
状态
):


我认为问题在于,您正在使用正在更改的值而不是实际的属性名称(即本例中的“状态”)来提升
OnPropertyChanged

我怀疑您还需要更改XAML以绑定到适当的属性(注意,属性名称是
State
而不是
State
-XAML将区分大小写):


我认为问题在于,您正在使用正在更改的值而不是实际的属性名称(即本例中的“状态”)来提升
OnPropertyChanged

我怀疑您还需要更改XAML以绑定到适当的属性(注意,属性名称是
State
而不是
State
-XAML将区分大小写):


我认为问题在于,您正在使用正在更改的值而不是实际的属性名称(即本例中的“状态”)来提升
OnPropertyChanged

我怀疑您还需要更改XAML以绑定到适当的属性(注意,属性名称是
State
而不是
State
-XAML将区分大小写):


我认为问题在于,您正在使用正在更改的值而不是实际的属性名称(即本例中的“状态”)来提升
OnPropertyChanged

我怀疑您还需要更改XAML以绑定到适当的属性(注意,属性名称是
State
而不是
State
-XAML将区分大小写):


您已经将页面的datacontext设置为
控件
,因此您的绑定

<TextBlock Name="Screen" Text="{Binding Path=control.state}></TextBlock>

您已经将页面的datacontext设置为
控件
,因此您的绑定

<TextBlock Name="Screen" Text="{Binding Path=control.state}></TextBlock>

您已经将页面的datacontext设置为
控件
,因此您的绑定

<TextBlock Name="Screen" Text="{Binding Path=control.state}></TextBlock>

您已经将页面的datacontext设置为
控件
,因此您的绑定

<TextBlock Name="Screen" Text="{Binding Path=control.state}></TextBlock>

而不是

public CardControler control { get; set; }
试试这个:

public CardControler control = new CardControler();
而不是

public CardControler control { get; set; }
试试这个:

public CardControler control = new CardControler();
而不是

public CardControler control { get; set; }
试试这个:

public CardControler control = new CardControler();
而不是

public CardControler control { get; set; }
试试这个:

public CardControler control = new CardControler();

OnPopertyChanged事件调用错误,必须将属性名称作为参数传递。您可以添加我在下面添加的代码。这样可以避免完全传递参数名

     public event PropertyChangedEventHandler PropertyChanged;

     protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
     {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
     }

OnPopertyChanged事件调用错误,必须将属性名称作为参数传递。您可以添加我在下面添加的代码。这样可以避免完全传递参数名

     public event PropertyChangedEventHandler PropertyChanged;

     protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
     {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
     }

OnPopertyChanged事件调用错误,必须将属性名称作为参数传递。您可以添加我在下面添加的代码。这样可以避免完全传递参数名

     public event PropertyChangedEventHandler PropertyChanged;

     protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
     {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
     }

OnPopertyChanged事件调用错误,必须将属性名称作为参数传递。您可以添加我在下面添加的代码。这样可以避免完全传递参数名

     public event PropertyChangedEventHandler PropertyChanged;

     protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
     {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
     }

到底是什么东西没有达到预期效果?我猜您的绑定已关闭,您通常会调用OnPropertyChanged(“PropertyName”)来告诉框架它需要更新该属性上的所有绑定。这意味着您需要修复对OnPropertyChanged的调用以及对PropertyChanged的方法。问题是什么