【问题标题】:How to update a listview from viewmodel binded model observable collection to itemsource如何将列表视图从视图模型绑定模型可观察集合更新到数据源
【发布时间】:2018-05-13 21:53:12
【问题描述】:

我有一个列表视图,其中还包含数据模板中的文本框,其中包含名称、地址、国家/地区等列。 现在这个 listview 的 Itemsource 绑定到我的 viewmodel 类中模型的可观察集合。

我正在更新 ObjModel(类型为ObservableCollection<Model>),方法是在 VM 中的某些条件下将名称和地址设为空,并且我可以在 viewmodel 类的 ObjModel 对象中看到名称的值,为空。 但是这些更改并没有反映在 UI(ListView)中,我错过了什么,如何更新列表视图。

我的视图是这样的:

 <DataTemplate x:Key="EquipmentItemTemplate">
                <Grid
                    x:Name="ListItem"
                    Height="40"
                    ZIndex="1">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="200" />
                        <ColumnDefinition Width="250" />
                        <ColumnDefinition Width="250" />
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Column="0" Text="{Binding Path=Name}" />
                    <TextBlock Grid.Column="1" Text="{Binding Path=Address}" />
                    <TextBlock Grid.Column="2" Text="{Binding Path=Country}" />
                </Grid>
            </DataTemplate>
 <ListView x:Name="MaintenanceElements" 
                 ItemContainerStyle="{StaticResource SequenceListItemStyle}"
                 ItemTemplate="{StaticResource EquipmentItemTemplate}"
                ItemsSource="{Binding EquipmentMaintenanceEntityItemsCollection}"
                SelectedItem="{Binding SelectedMaintenanceElement}">
                    <ListView.View>
                        <GridView AllowsColumnReorder="False">
                            <GridViewColumn
                                    Width="200"
                                    local:GridViewSort.PropertyName="Name"
                                    Header="Name" />
                            <GridViewColumn
                                    Width="250"
                                    local:GridViewSort.PropertyName="Address"
                                    Header="Address" />
                            <GridViewColumn
                                    Width="250"
                                    local:GridViewSort.PropertyName="Country"
                                    Header="Country" />

                        </GridView>
                    </ListView.View>                   
                </ListView>

视图模型包含:

 public ObservableCollection<Model> ObjModel { get; set; }

在某些情况下我会做一些事情

ObjModel[0].Name= string.Empty; 

在 ListView 中不更新,因为它的 itemsource 绑定到 Model 对象 observable 集合,如何从这里更新 ListView?

我的模型是:

public class EquipmentMaintenanceModel : ChangeTracker, INotifyPropertyChanged
    {
      private string name;
      private string address;
      private string country;

       public string Name
        {
            get { return this.name; }
            set { this.name = value; }
        }

       public string Address
        {
            get { return this.address; }
            set { this.address = value; }
        }
       public string Country
        {
            get { return this.country; }
            set { this.country = value; }
        }
        private void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

【问题讨论】:

    标签: wpf listview mvvm data-binding itemsource


    【解决方案1】:

    在您的模型中,您需要在设置属性值时触发 PropertyChanged。例如:

    public string Name
      {
        get { return this.name; }
        set 
        {
          if(this.name != value)
          {
            this.name = value;
            OnPropertyChanged(Name);
          }
        }
      }
    

    【讨论】:

      猜你喜欢
      • 2020-01-21
      • 2013-09-25
      • 2013-04-05
      • 2014-03-12
      • 1970-01-01
      • 2016-12-26
      • 2012-11-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多