C# 在运行时更改ObservableCollection以进行绑定

C# 在运行时更改ObservableCollection以进行绑定,c#,wpf,data-binding,observablecollection,C#,Wpf,Data Binding,Observablecollection,我正在为我的ObservableCollection中的每个项目生成网格。现在我希望能够在运行时更改源集合,但我不确定需要做什么 这是我的XAML: <Window.Resources> <c:GraphicsList x:Key="GraphicsData" /> </Window.Resources> ... ... <ItemsControl x:Name="icGraphics" ItemsSource="{Binding Source={

我正在为我的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>
myCollection1:

public class GraphicsList : ObservableCollection<Graphics>
{
    public GraphicsList()
    {

    }
}

这是我代码的简化版本,但它可以工作,我基本上希望将源集合myCollection1更改为myCollection2(它是同一个类,只是不同的列表)。如何执行此操作?

您可以按如下所示从集合中添加或删除项目

        var dresource = this.Resources["GraphicsData"] as GraphicsList;
        dresource.Add(new Graphics() { Name = "New Entry" });
但使用StaticResource,您无法将新集合分配给ResourceDictionary中的集合

理想情况下,如果要分配全新的集合,应该使用ViewModel和bind集合

您的mainwindow类或viewmodel应实现INotifyPropertyChanged接口

示例代码

    public partial class MainWindow : Window, INotifyPropertyChanged
{
    private GraphicsList _graphicsData;

    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
        this.Loaded += MainWindow_Loaded;
    }

    public GraphicsList GraphicsData
    {
        get { return _graphicsData; }
        set
        {
            if (Equals(value, _graphicsData)) return;
            _graphicsData = value;
            OnPropertyChanged("GraphicsData");
        }
    }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        //var resource = this.Resources["GraphicsData"] as GraphicsList;


        var resource = new GraphicsList();
        resource.Add(new Graphics(){Name = "Some new Collection of data"});

        this.GraphicsData = resource;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}
还有你的Xaml

    <Grid>
    <ListBox ItemsSource="{Binding GraphicsData}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}"></TextBlock>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>


我希望这将有助于

您可以按如下方式从收藏中添加或删除项目

        var dresource = this.Resources["GraphicsData"] as GraphicsList;
        dresource.Add(new Graphics() { Name = "New Entry" });
但使用StaticResource,您无法将新集合分配给ResourceDictionary中的集合

理想情况下,如果要分配全新的集合,应该使用ViewModel和bind集合

您的mainwindow类或viewmodel应实现INotifyPropertyChanged接口

示例代码

    public partial class MainWindow : Window, INotifyPropertyChanged
{
    private GraphicsList _graphicsData;

    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
        this.Loaded += MainWindow_Loaded;
    }

    public GraphicsList GraphicsData
    {
        get { return _graphicsData; }
        set
        {
            if (Equals(value, _graphicsData)) return;
            _graphicsData = value;
            OnPropertyChanged("GraphicsData");
        }
    }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        //var resource = this.Resources["GraphicsData"] as GraphicsList;


        var resource = new GraphicsList();
        resource.Add(new Graphics(){Name = "Some new Collection of data"});

        this.GraphicsData = resource;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}
还有你的Xaml

    <Grid>
    <ListBox ItemsSource="{Binding GraphicsData}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}"></TextBlock>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>


我希望这将有助于

您可以按如下方式从收藏中添加或删除项目

        var dresource = this.Resources["GraphicsData"] as GraphicsList;
        dresource.Add(new Graphics() { Name = "New Entry" });
但使用StaticResource,您无法将新集合分配给ResourceDictionary中的集合

理想情况下,如果要分配全新的集合,应该使用ViewModel和bind集合

您的mainwindow类或viewmodel应实现INotifyPropertyChanged接口

示例代码

    public partial class MainWindow : Window, INotifyPropertyChanged
{
    private GraphicsList _graphicsData;

    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
        this.Loaded += MainWindow_Loaded;
    }

    public GraphicsList GraphicsData
    {
        get { return _graphicsData; }
        set
        {
            if (Equals(value, _graphicsData)) return;
            _graphicsData = value;
            OnPropertyChanged("GraphicsData");
        }
    }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        //var resource = this.Resources["GraphicsData"] as GraphicsList;


        var resource = new GraphicsList();
        resource.Add(new Graphics(){Name = "Some new Collection of data"});

        this.GraphicsData = resource;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}
还有你的Xaml

    <Grid>
    <ListBox ItemsSource="{Binding GraphicsData}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}"></TextBlock>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>


我希望这将有助于

您可以按如下方式从收藏中添加或删除项目

        var dresource = this.Resources["GraphicsData"] as GraphicsList;
        dresource.Add(new Graphics() { Name = "New Entry" });
但使用StaticResource,您无法将新集合分配给ResourceDictionary中的集合

理想情况下,如果要分配全新的集合,应该使用ViewModel和bind集合

您的mainwindow类或viewmodel应实现INotifyPropertyChanged接口

示例代码

    public partial class MainWindow : Window, INotifyPropertyChanged
{
    private GraphicsList _graphicsData;

    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
        this.Loaded += MainWindow_Loaded;
    }

    public GraphicsList GraphicsData
    {
        get { return _graphicsData; }
        set
        {
            if (Equals(value, _graphicsData)) return;
            _graphicsData = value;
            OnPropertyChanged("GraphicsData");
        }
    }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        //var resource = this.Resources["GraphicsData"] as GraphicsList;


        var resource = new GraphicsList();
        resource.Add(new Graphics(){Name = "Some new Collection of data"});

        this.GraphicsData = resource;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}
还有你的Xaml

    <Grid>
    <ListBox ItemsSource="{Binding GraphicsData}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}"></TextBlock>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>


我希望这将有助于

您必须更改GraphicsData它将在UI上反射创建一个带有GraphicsList属性(带有更改通知)的视图模型类,并将ItemsSource绑定到该属性。根据您所追求的内容和您希望它发生的时间,
icGraphics.ItemsSource=myCollection2
将简单地实现这一点。您正在寻找XAML中的解决方案?您必须更改GraphicsData,它将在UI上反射创建一个具有GraphicsList属性(带有更改通知)的视图模型类,并将ItemsSource绑定到该属性。具体取决于您所要做的事以及您希望它发生的时间,
icGraphics.ItemsSource=myCollection2
将简单地完成这一任务。您正在寻找XAML中的解决方案?您必须更改GraphicsData,它将在UI上反射创建一个具有GraphicsList属性(带有更改通知)的视图模型类,并将ItemsSource绑定到该属性。具体取决于您所要做的事以及您希望它发生的时间,
icGraphics.ItemsSource=myCollection2
将简单地完成这一任务。您正在寻找XAML中的解决方案?您必须更改GraphicsData,它将在UI上反射创建一个具有GraphicsList属性(带有更改通知)的视图模型类,并将ItemsSource绑定到该属性。具体取决于您所要做的事以及您希望它发生的时间,
icGraphics.ItemsSource=myCollection2
将简单地完成这一任务。您正在寻找XAML中的解决方案?这对我真的很有帮助,我抛弃了静态资源,它可以正常工作。感谢这真的帮助了我,我放弃了静态资源,它工作了。感谢这真的帮助了我,我放弃了静态资源,它工作了。感谢这真的帮助了我,我放弃了静态资源,它工作了。谢谢