INotifyPropertyChanged和DataBinding

时间:2015-01-27 14:30:50

标签: c# wpf xaml

我正在制作一个测试应用程序来处理CardReader,我有一个带有CardReader状态的枚举和带有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>

我希望我能正确解释自己,有人可以帮助我。 提前致谢

7 个答案:

答案 0 :(得分:2)

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

PropertyChanged(this, new PropertyChangedEventArgs(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}不同。

答案 1 :(得分:1)

我认为问题在于你提升OnPropertyChanged正在改变,而不是实际的属性名称(即&#34;状态&#34;在此情况)。

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

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

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

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

绑定中属性的情况需要与公共属性匹配(State,而不是state):

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

答案 4 :(得分:0)

您已将页面的datacontext设置为control,因此您的绑定

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

将是错误的。

你的装订应该是

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

答案 5 :(得分:0)

而不是

public CardControler control { get; set; }

试试这个:

public CardControler control = new CardControler();

答案 6 :(得分:0)

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

     public event PropertyChangedEventHandler PropertyChanged;

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