0
我將盡可能簡化所有事物的清理工作。可以說我有電流型號:刷新查找列表的數據源
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與此類似:(來源是通過視圖模型暴露型產品的公共屬性,組列表是集團,也通過視圖模型暴露的ObservableCollection):
<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會導致重置當前產品的GroupId,通過將其設置爲NULL。
爲什麼會發生這種情況,除了在列表刷新後手動設置它以外,處理此問題的最佳方法是什麼? GroupId屬性被更改的事實並不好,因爲PropertyChanged可能會對此屬性進行一些操作。出於這個原因,我還需要引入一個私人bool變量_isRefreshing Groups,這樣我就可以防止一些不必要的操作,而這一切都會「污染」代碼。
對不起,什麼是'GroupList'? – sll 2012-07-31 12:35:23
我已經寫過這個問題,GroupList是ObservableCollection,包含一個Group項目列表。 –
Goran
2012-07-31 12:36:42
對不起,忽略了 – sll 2012-07-31 13:00:36