2

我在谷歌上搜索了一个解决方案,但我无法在这里应用我发现的问题,所以我有一个带有静态属性的静态类,我希望将 ListView 静态属性绑定到 ObservableCollection 属性,这已经完成但现在我想通知 ListView ObservableCollection 中发生的更改

 public static class Global
{
   public static ObservableCollection<Dictionary<string,Dictionary<string,object>>> operations;
   ObservableCollection<String> names = new ObservableCollection<string>();
   public static ObservableCollection<String> _Operations
   {
       get
       {               

           for (int i = 0; i < Global.operations.Count; i++)
           {
               if (Global.operations.Count != 0)
               {
                   names.Add(Global.operations[i].Keys.First().ToString());
               }
           }
           return names;
       }
       set
       {
           if (_Operations != value)
           {
               _Operations = value;
           }
       }

   }

}

然后是 XAML

<ListView Name="list"  ItemsSource="{Binding Source={x:Static s:Global._Operations}}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding }"  />
                    <Button Content="Delete" Click="Button_Click"/>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
        </ListView>

这是删除的代码,我确定它添加和删除,因为我可以立即看到“操作”被添加或删除的效果

var item = ((Button)sender).DataContext;
        var itemIndex = list.Items.IndexOf(item);
        Global.operations.RemoveAt(itemIndex);
        list.Items.Refresh();

这绑定并显示,显然当我删除/添加项目到我的静态 ObservableCollection 时,我需要更新 ListView。提前致谢

4

3 回答 3

0

使用您的代码设置方式,我能想到的强制刷新的唯一方法是将 ItemsSource 设置为 null,然后将 ItemsSource 设置回来

您正在更改操作,但不是列表视图绑定到的 _Operations 是问题所在。使用下面的代码,它应该可以工作。

    var item = ((Button)sender).DataContext;
    var itemIndex = list.Items.IndexOf(item);
    Global.operations.RemoveAt(itemIndex);
    list.ItemsSource = null;
    list.ItemsSource = Global._Operations;
于 2013-05-25T23:37:07.467 回答
0

您可以更改Global为单身人士并实施INotifyPropertyChanged. 像这样的东西:

public class Global : INotifyPropertyChanged
{
    private static Global _instance = new Global();
    public static Global Instance { get { return _instance; } }

    private ObservableCollection<string, Dictionary<string, Dictionary<string, object>>> operations = new ObservableCollection<string, Dictionary<string, Dictionary<string, object>>>();

    public ObservableCollection<string> _Operations
    {
        get
        {
            // ...
        }
        set
        {
            // ...
            OnPropertyChanged("_Operations");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propName)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(propName);
    }
}

您将在绑定中设置如下:

<ListView Name="list" DataContext="{x:Static Global.Instance}" ItemsSource="{Binding Path=_Operations}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding }"  />
                <Button Content="Delete" Click="Button_Click"/>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
于 2013-05-25T23:14:56.570 回答
0

如果您将 Global 类替换为以下内容,则对操作对象所做的添加/删除将传播到 _names 并且绑定将正常工作。

如果您需要分配给 Global.operation(例如,需要订阅新的集合 CollectionChanged 事件),则需要额外的代码,但大概您可以修改现有代码以简单地从 Global.operation 添加/删除。

public static class Global
{
    public static ObservableCollection<Dictionary<string, Dictionary<string, object>>> operations = new ObservableCollection<Dictionary<string, Dictionary<string, object>>>();

    static ObservableCollection<String> names = new ObservableCollection<string>();
    public static ObservableCollection<String> _Operations
    {
        get
        {               
            return names;
        }
    }

    static Global()
    {
        operations.CollectionChanged += operations_CollectionChanged;
    }

    static void operations_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        if (e.OldItems != null)
        {
            foreach (Dictionary<string, Dictionary<string, object>> value in e.OldItems)
            {
                _Operations.Remove(value.Keys.First().ToString( ));
            }
        }
        if (e.NewItems != null)
        {
            foreach (Dictionary<string, Dictionary<string, object>> value in e.NewItems)
            {
                _Operations.Add(value.Keys.First().ToString());
            }
        }
    }
}
于 2013-05-31T04:39:35.920 回答