<legend id='MhaXi'><style id='MhaXi'><dir id='MhaXi'><q id='MhaXi'></q></dir></style></legend>
      <tfoot id='MhaXi'></tfoot>

        <bdo id='MhaXi'></bdo><ul id='MhaXi'></ul>

        <small id='MhaXi'></small><noframes id='MhaXi'>

      1. <i id='MhaXi'><tr id='MhaXi'><dt id='MhaXi'><q id='MhaXi'><span id='MhaXi'><b id='MhaXi'><form id='MhaXi'><ins id='MhaXi'></ins><ul id='MhaXi'></ul><sub id='MhaXi'></sub></form><legend id='MhaXi'></legend><bdo id='MhaXi'><pre id='MhaXi'><center id='MhaXi'></center></pre></bdo></b><th id='MhaXi'></th></span></q></dt></tr></i><div id='MhaXi'><tfoot id='MhaXi'></tfoot><dl id='MhaXi'><fieldset id='MhaXi'></fieldset></dl></div>

        WPF 列表框绑定更新

        时间:2023-10-07
        <legend id='IGiNZ'><style id='IGiNZ'><dir id='IGiNZ'><q id='IGiNZ'></q></dir></style></legend>

          1. <small id='IGiNZ'></small><noframes id='IGiNZ'>

                  <bdo id='IGiNZ'></bdo><ul id='IGiNZ'></ul>

                • <i id='IGiNZ'><tr id='IGiNZ'><dt id='IGiNZ'><q id='IGiNZ'><span id='IGiNZ'><b id='IGiNZ'><form id='IGiNZ'><ins id='IGiNZ'></ins><ul id='IGiNZ'></ul><sub id='IGiNZ'></sub></form><legend id='IGiNZ'></legend><bdo id='IGiNZ'><pre id='IGiNZ'><center id='IGiNZ'></center></pre></bdo></b><th id='IGiNZ'></th></span></q></dt></tr></i><div id='IGiNZ'><tfoot id='IGiNZ'></tfoot><dl id='IGiNZ'><fieldset id='IGiNZ'></fieldset></dl></div>

                    <tbody id='IGiNZ'></tbody>
                • <tfoot id='IGiNZ'></tfoot>
                  本文介绍了WPF 列表框绑定更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我是 WPF 的新手,我只是设法将特定类项的列表绑定到 ListBox.ListBox 现在成功地显示它们.这是一些代码,首先是类:

                  I'm brand new to WPF and I just managed to bind a list of specific class items to a ListBox. The ListBox now successfully displays them. Here's some code, the class first:

                  public class OrderItem
                  {
                      public int Quantity { get; set; }
                      public string Name { get; set; }
                      public Double Price { get; set; }
                  }
                  

                  一些虚拟数据和绑定,这一切都发生在主程序的构造函数中:

                  Some dummy data and the binding, which all happens in the constructor of the main program:

                  List<OrderItem> currentOrderItems = new List<OrderItem>();
                          currentOrderItems.Add(new OrderItem() { Quantity = 5, Name = "Test", Price = 5 });
                          currentOrderItems.Add(new OrderItem() { Quantity = 15, Name = "Test test", Price = 6.66 });
                          currentOrderItems.Add(new OrderItem() { Quantity = 1, Name = "Test 3", Price = 15.88 });
                          listOrderItems.ItemsSource = currentOrderItems;
                  

                  还有 XAML:

                  <ListBox HorizontalAlignment="Left" Margin="150,27,0,23" Name="listOrderItems" Width="150" FontFamily="Times New Roman" FontSize="12">
                          <ListBox.ItemTemplate>
                              <DataTemplate>
                                  <Grid Margin="4">
                                      <Grid.ColumnDefinitions>
                                          <ColumnDefinition Width="Auto" />
                                          <ColumnDefinition Width="Auto" />
                                          <ColumnDefinition Width="Auto" />
                                      </Grid.ColumnDefinitions>
                                      <TextBlock Text="{Binding Quantity}" FontWeight="Bold"  />
                                      <TextBlock Grid.Column="1" Text="{Binding Name }" />
                                      <TextBlock Grid.Column="2" Text="{Binding Price }" />
                                  </Grid>
                              </DataTemplate>
                          </ListBox.ItemTemplate>
                      </ListBox>
                  

                  我的问题是如何更新 ListBox 以显示我添加到包含数据(又名 OrderItems)的列表中的新项目.如果我以任何方式添加、删除或修改列表,这不会反映在 ListBox 中.谢谢!

                  My question is how can I update the ListBox to display a new item that I'm adding to the List containing the data a.k.a. the OrderItems. If I add, remove or modify the List in any way, this is not reflected in the ListBox. Thanks!

                  推荐答案

                  对于您的情况,最好的方法是使用 mvvm 模式.简单来说:您的模型 OrderItem 应该实现接口 INotifyPropertyChanged.如果属性发生变化,它会通知这一点.然后创建视图模型并将其设置在数据上下文中.在 viewModel 中添加 ObservableCollection 和 OrderItems,这个集合应该通知视图它的变化.有关更多信息,请阅读以下文章:MVVM

                  For your case best way use mvvm patern. In brief: Your model OrderItem should implement interface INotifyPropertyChanged. If property changed, it notified about this. Then create viewmodel and set it in datacontext. In viewModel add ObservableCollection with OrderItems, this collection should notified view about changes in it. for more information read some articles like this: MVVM

                  这篇关于WPF 列表框绑定更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:我想在 winforms 列表框控件中检测项目双击.[如何处理点击空白区域?] 下一篇:列表框项删除

                  相关文章

                    <small id='TJ2MC'></small><noframes id='TJ2MC'>

                    <i id='TJ2MC'><tr id='TJ2MC'><dt id='TJ2MC'><q id='TJ2MC'><span id='TJ2MC'><b id='TJ2MC'><form id='TJ2MC'><ins id='TJ2MC'></ins><ul id='TJ2MC'></ul><sub id='TJ2MC'></sub></form><legend id='TJ2MC'></legend><bdo id='TJ2MC'><pre id='TJ2MC'><center id='TJ2MC'></center></pre></bdo></b><th id='TJ2MC'></th></span></q></dt></tr></i><div id='TJ2MC'><tfoot id='TJ2MC'></tfoot><dl id='TJ2MC'><fieldset id='TJ2MC'></fieldset></dl></div>

                      <bdo id='TJ2MC'></bdo><ul id='TJ2MC'></ul>

                    1. <tfoot id='TJ2MC'></tfoot>
                    2. <legend id='TJ2MC'><style id='TJ2MC'><dir id='TJ2MC'><q id='TJ2MC'></q></dir></style></legend>