gpt4 book ai didi

c# - 如何在绑定(bind)到 ObjectDataProvider 时删除 ListView 中的所有项目

转载 作者:行者123 更新时间:2023-11-30 15:07:12 26 4
gpt4 key购买 nike

我将一个 ListView 绑定(bind)到 ObjectDataProvider。我从用户那里获取了一些值并在运行时更改了我的 ObjectDataProvider,但是当我的 ObjectDataProvider 更新了它的所有 Item 添加到 ListView 并替换它们时。我使用这个语句:

lstUsers.Items.Clear();

但是我得到这个错误:

Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead.

当它绑定(bind)到 ObjectDataProvider 时,如何从 ListView 中删除所有数据?

谢谢

编辑 1):这是我的代码:

public partial class Page_ObjectDataProvider : Window
{
public Page_ObjectDataProvider()
{
InitializeComponent();
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{

}

private void button1_Click(object sender, RoutedEventArgs e)
{
int myValue =10;
((ObjectDataProvider)this.FindResource("ADUsers")).MethodParameters.Clear();
((ObjectDataProvider)this.FindResource("ADUsers")).MethodParameters.Add(myValue);
((ObjectDataProvider)this.FindResource("ADUsers")).Refresh();
}
}

public class CustomData
{
public int F1 { get; set; }
public int F2 { get; set; }
public string F3 { get; set; }
}

public class RetrievCustomData : List<CustomData>
{
public RetrievCustomData GetSome(int i)
{
for (int j = 0; j < i; j++)
{
CustomData cd = new CustomData();
Random rnd = new Random();
cd.F1 = j;
cd.F2 = rnd.Next(i);
cd.F3 = "nima";
this.Add(cd);
}

return this;
}
}

和 XAML:

<Window x:Class="TestWPF.Page_ObjectDataProvider"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:TestWPF"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="ObjectDataProvider" Height="362" Width="360" Loaded="Window_Loaded">
<Window.Resources>
<ObjectDataProvider x:Key="ADUsers" ObjectType="{x:Type src:RetrievCustomData}"
MethodName="GetSome">
<ObjectDataProvider.MethodParameters>
<sys:Int32>20</sys:Int32>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
<Grid>
<ListView x:Name="lstUsers"
ItemsSource="{Binding Source={StaticResource ADUsers}}" Margin="0,0,0,106">
<ListView.View>
<GridView>
<GridViewColumn Header="User Name"
Width="80"
DisplayMemberBinding="{Binding Path=F1}" />
<GridViewColumn Header="Group Distinguished Name"
Width="80"
DisplayMemberBinding="{Binding Path=F3}" />
<GridViewColumn Header="Group Distinguished Name"
Width="80"
DisplayMemberBinding="{Binding Path=F2}" />
</GridView>
</ListView.View>
</ListView>
<Button Content="Get" Height="58" HorizontalAlignment="Left" Margin="64,253,0,0" Name="button1" VerticalAlignment="Top" Width="179" Click="button1_Click" />
</Grid>

如果我将 DataContext 或我的 ObjectDataProvider 设置为空,那么它不会再次绑定(bind)。只是我想更新 ObjectDataProvider 并将新值绑定(bind)到我的 ListView

最佳答案

您可以清除 ListView 的 ItemsSource 属性以清除项目。

lstUsers.ClearValue(ListView.ItemsSourceProperty);

关于c# - 如何在绑定(bind)到 ObjectDataProvider 时删除 ListView 中的所有项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6882782/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com