【发布时间】:2014-07-03 12:31:12
【问题描述】:
我正在为我的 ObservableCollection 中的每个项目生成网格。现在我希望能够在运行时更改源集合,但我不确定需要做什么。
这是我的 XAML:
<Window.Resources>
<c:GraphicsList x:Key="GraphicsData" />
</Window.Resources>
...
...
<ItemsControl x:Name="icGraphics" ItemsSource="{Binding Source={StaticResource GraphicsData}}" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Tag="{Binding id}" Margin="15,0,15,15">
<Label Grid.Row="0" Content="{Binding name}"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
和 C#:
myCollection1 = this.FindResource("GraphicsData") as GraphicsList;
我的Collection1:
public class GraphicsList : ObservableCollection<Graphics>
{
public GraphicsList()
{
}
}
图形类:
class Graphics: INotifyPropertyChanged
{
// some properties not important
}
它是我的代码的简化版本,但它可以工作,我基本上想将源集合 myCollection1 更改为 myCollection2(这是同一个类,只是不同的列表)。我该怎么做?
【问题讨论】:
-
你必须改变 GraphicsData 它会反映在 UI 上
-
创建一个具有 GraphicsList 属性的视图模型类(带有更改通知)并将 ItemsSource 绑定到该属性。
-
根据您所追求的以及您希望它何时发生,
icGraphics.ItemsSource = myCollection2将轻松解决问题。您正在寻找 XAML 中的解决方案?
标签: c# wpf data-binding observablecollection