【问题标题】:Changing ObservableCollection at runtime for binding在运行时更改 ObservableCollection 以进行绑定
【发布时间】: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


【解决方案1】:

您可以按以下方式从集合中添加或删除项目

        var dresource = this.Resources["GraphicsData"] as GraphicsList;
        dresource.Add(new Graphics() { Name = "New Entry" });

但是使用 StaticResource,您不能将新的 Collection 分配给 ResourceDictionary 中的一个。

如果您想分配全新的集合,理想情况下您应该使用 ViewModel 并绑定 Collection。

您的主窗口类或视图模型应实现 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>

我希望这会有所帮助

【讨论】:

  • 这对我很有帮助,我放弃了静态资源并且它可以工作。谢谢
猜你喜欢
  • 2017-11-21
  • 1970-01-01
  • 2017-09-23
  • 2014-07-26
  • 2012-06-04
  • 1970-01-01
  • 1970-01-01
  • 2016-11-28
  • 2016-04-22
相关资源
最近更新 更多