简体   繁体   中英

INotifyPropertyChanged and DataBinding

I'm making a test application to handle a CardReader, I have an enum with the states of the CardReader and a XAML window with a TextBlock, i want that when the state change the onPropertyChanged change the TextBlock with the name of the state.

Here is part of my code:

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

    }

And in the xaml

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

I hope i explained my self correctly and somebody can help me. Thanks in advance

The following line is incorrect as you should pass propertyName as a parameter instead of state.ToString() :

PropertyChanged(this, new PropertyChangedEventArgs(state.ToString()));

So your code should look something like:

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

In addition to this keep in mind that xaml is case sensitive so {Binding State} is not the same as {Binding state} .

I think the problem is that you are raising the OnPropertyChanged with the value that is changing, rather than the actual property name (ie "State" in this case).

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

I suspect you also need to change your XAML to bind to the appropriate property (note, the property name is State not state - the XAML will be case sensitive):

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

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

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

您绑定中的属性的大小写需要匹配公共属性( State ,而不是state ):

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

You have alread set the datacontext of the page to control so your binding

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

will be wrong.

your binding should be

<TextBlock Name="Screen" Text="{Binding Path=State}></TextBlock>

Instead of

public CardControler control { get; set; }

try this:

public CardControler control = new CardControler();

your OnPopertyChanged Event Call is wrong , you have to pass Property name as the argumeny. you can add the code i added below. that way you can avoid passing the parameter name altogether.

     public event PropertyChangedEventHandler PropertyChanged;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM