首页 > 解决方案 > Itemsource 未更新 (wpf,c#)

问题描述

我在读取更改后的值时遇到问题:

XAML 中的绑定:

  Text="{Binding Path=Infotext,Mode=TwoWay}"

我尝试通过以下方式读取一个值(它绑定到一个文本框,类是票证,ic_Tickets 是一个带有“票证”的 Itemscontrol):

string text=((ObservableCollection<Ticket>)ic_Tickets.ItemsSource).Where(Ticket => Ticket.id == ((MenuItem)sender).Tag.ToString()).First().Infotext;

有了这个(在公共类 Ticket:INotifyPropertyChanged 我得到了旧的绑定值

public string Infotext { get
            {
                return Infotext_int;
            }


            set {
                Infotext_int = value;
             
                NotifyPropertyChanged();
            } }

但如果我添加这样的消息框:

 public string Infotext { get
            {
                return Infotext_int;
            }


            set {
                Infotext_int = value;
             MessageBox.Show("!!!"); //or inside the NotifyPropertyChanged()
                NotifyPropertyChanged();
            } }

我得到了新的价值......(我想要新的价值,但它不可能总是显示一个消息框)

因此,只有当消息框显示 oO 时,它才会将新值设置为 Itemssource

通知功能:

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

标签: c#wpfitemsource

解决方案


推荐阅读