【问题标题】:Observable Collection is not being Updated (observable collection of strings inside an observable collection of class item)Observable Collection 未更新(可观察的类项集合内的字符串的可观察集合)
【发布时间】:2021-12-21 06:50:27
【问题描述】:

我有 2 个名为 person 和 Remainder 的类

这是剩余类的详细信息,它包含一个可观察的集合名称 zuids

        public string Description;
        public DateTime DateAndTime;
        public string dateandtime;
        public bool isCompleted;
        public ObservableCollection<string> zuids;
        public string channelId;

这是个人类

 public string zuid;
        public string name,email;
        public Remainder r { get; set; }
        public Person(string id,string name,string email)
        {
            this.zuid = id;
            this.name = name;
            this.email = email;
        }

我刚刚创建了这个 UI

主页

public List<Person> Persons = new List<Person>();
 public List<Remainder> Remainders = new List<Remainder>();

 public ObservableCollection<Remainder> RemaindersForMe = new ObservableCollection<Remainder>();

(这些对我来说的剩余部分显示在图片右侧的剩余列表中)

所以在弹出框中,我有一个添加按钮,可以将用户添加到其余部分,因此,当我从自动建议框中选择某个人的名称时,他们各自的 id 会更新到剩余部分 zuids 可观察集合中。但是当我添加它时,它没有被更新,因为我试图用旧的值创建新的余数,并且新的余数更新的人会正确显示。

这是自动提示框 这是我的代码

<Button Name="AddMorePeople" Content="+" CornerRadius="25" FontSize="20">
                                                        <Button.Flyout>
                                                            <Flyout>
                                                                <StackPanel >
                                                                    <AutoSuggestBox 
                                                                        Name="MyAutoSuggestBox1" 
                                                                        PlaceholderText="Search"  
                                                                        Width="150"
                                                                        SuggestionChosen="MyAutoSuggestBox1_SuggestionChosen"
                                                                        TextChanged="MyAutoSuggestBox_TextChanged"
                                                                        PointerEntered="MyAutoSuggestBox_PointerEntered"
                                                                        Tag="{x:Bind RemainderId}"
                                                                     />
                                                                </StackPanel>
                                                            </Flyout>
                                                        </Button.Flyout>
                                                    </Button>

这是图片中显示的添加按钮,单击添加按钮会打开自动建议框

private void MyAutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
        {
            var Auto = (AutoSuggestBox)sender;
            List<string> Suggestion;
            if (string.IsNullOrEmpty(Auto.Text))
                Suggestion = Peoplenames.Where(p=>p!="excludedname").ToList();
            else
                Suggestion = Peoplenames.Where(p => p.StartsWith(Auto.Text, StringComparison.OrdinalIgnoreCase) && p!="excludedname" ).ToList();
            Auto.ItemsSource = Suggestion.ToArray();
            
        }

private void MyAutoSuggestBox1_SuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args)
        {
            int value = (int)sender.Tag;
            
            foreach(var i in RemaindersForMe.ToList())
            {
                if(i.RemainderId==value)
                {
                    foreach(var j in Persons)
                    {
                        if(j.name==args.SelectedItem.ToString())
                        {
                            i.zuids.Add(j.zuid);
                            //Testingbox.Text = i.zuids.Count.ToString();
                            break;
                        }
                    }
                    break;
                }
            }
}

在测试框中,用户数正常显示,但在remianders列表中没有更新。

请帮忙 提前致谢

【问题讨论】:

  • 请提供最少的可重现示例,至少是带有绑定和初始化代码的 XAML。
  • @AlexeyRumyantsev Hai 我更新了请看一下
  • public ObservableCollection&lt;string&gt; zuids; 我没有看到任何代码实例化该可观察的集合,是代码i.zuids.Count.ToString() 抛出NullReferenceException?
  • 让我帮你!
  • 在Skype上联系我:shakir_820

标签: c# data-binding uwp uwp-xaml observablecollection


【解决方案1】:

您必须使用 INotifyProperyChanged 来反映更改。您还必须使用 x:Bind, Mode=TwoWay。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多