我在下面有一個用戶串列:
public ObservableCollection<User> Users
{
get;
private set;
}
在 XAML 檔案中,我將用戶系結到網格控制元件
<dxg:GridControl x:Name="grid"
ItemsSource="{Binding Users}"
SelectedItem="{Binding CurrentUser}"
...
>
<dxg:GridControl.Columns>
<dxg:GridColumn FieldName="Name" Header="name" />
<dxg:GridColumn FieldName="Mobile" Header="mobile"/>
</dxg:GridControl.Columns>
</dxg:GridControl>
直到這里一切正常,系結作業正常。
現在我想系結特殊用戶。例如,姓名長度小于 5 的用戶。
private ObservableCollection<TUserItem> usersVisible;
public ObservableCollection<User> UsersVisible
{
get
{
return Users.Where(u => u.name.Length < 5).ToObservableCollection();
}
set
{
usersVisible = value;
OnPropertyChanged();
}
}
但它不起作用并且 XAML 網格控制元件不會更新。
如果我回來
return Users;
代替
return Users.Where(u => u.name.Length < 5).ToObservableCollection();
一切正常,系結作業正常。
你有什么建議?
謝謝
uj5u.com熱心網友回復:
如果你查看 ToObservableCollection 方法的源代碼,你會發現它回傳了一個新的集合。所以你的系結現在仍然指向舊物件,你不會看到任何變化。我為您創建了一個 WPF 示例的存盤庫,您可以在此處運行:

請注意,我沒有使用可觀察集合,而是使用普通串列。您可以在 SO: Binding a CollectionViewSource to ObservableCollection的這個答案中看到使用集合視圖源的可觀察集合示例
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/461936.html
上一篇:UWP使用DataTemplate在GridView中顯示影像
下一篇:屬性值更改時更改影像源
