【问题标题】:Refreshing data source for lookup list刷新查找列表的数据源
【发布时间】:2012-07-31 21:16:19
【问题描述】:

我将简化事情以使一切尽可能清晰。假设我有当前的模型:

public class Product
{
    public int Id {get;set;}
    public string Name {get;set;}
    public int GroupId {get;set;}
    public Group Group {get;set;}
}

public class Group
{
    public int Id {get;set;}
    public string Name {get;set;}
}

假设我有类似这样的 xaml:(Source 是通过 ViewModel 公开的 Product 类型的公共属性,GroupList 是 Group 的 ObservableCollection,也通过 ViewModel 公开):

<TextBlock Text="Id" />
<TextBox Grid.Column="1" Text="{Binding Source.Id}" />
<TextBlock Grid.Row="1" Text="Name" />
<TextBox Grid.Column="1" Grid.Row="1" Text="{Binding Source.Name}" />
<TextBlock Grid.Row="2" Text="Group" />
<ComboBox Grid.Column="1" Grid.Row="2"
          DisplayMemberPath="Name"
          ItemsSource="{Binding GroupList}"
          SelectedValue="{Binding Source.GroupId}"
          SelectedValuePath="Id"
          />

这一切都很好。但是,想象一下,出于某种原因,我需要刷新 GroupList 中的项目。调用 ViewModel 上的 ICommand,获取数据,并将 GroupList 设置为新列表。看来刷新 GroupList 会导致当前 Product 的 GroupId 重置为 NULL。

为什么会发生这种情况,除了在列表刷新后手动设置之外,最好的处理方法是什么? GroupId 属性被更改的事实并不好,因为该属性的 PropertyChanged 可能有一些操作。出于这个原因,我还需要引入一个私有 bool 变量 _isRefreshing Groups,这样我就可以阻止一些根本不需要的操作,而这一切都会“污染”代码。

【问题讨论】:

  • 我在问题里写过,GroupList是ObservableCollection,包含一个Group项的列表。

标签: wpf data-binding wpf-controls


【解决方案1】:

抱歉,您必须在刷新收藏后手动设置 GroupId。使用一个属性来指示集合是否正在刷新的想法是一个很好的想法(IsBusy 或 IsRefreshing 是我的建议)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-11
    • 2023-01-31
    • 2020-10-21
    • 2021-10-25
    • 2016-01-11
    • 2021-12-02
    • 1970-01-01
    • 2015-03-29
    相关资源
    最近更新 更多