我在谷歌上搜索了一个解决方案,但我无法在这里应用我发现的问题,所以我有一个带有静态属性的静态类,我希望将 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。提前致谢