【WPF】当 ItemsSource 正在使用时操作无效。改用 ItemsControl.ItemsSource 访问和修改元素...

问题:

  • 中文版报错:Additional information: 当 ItemsSource 正在使用时操作无效。改用 ItemsControl.ItemsSource 访问和修改元素。
  • 英文版报错:Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead.

原因:前台XAML中ListBox之类的含多个条目的控件,它的ItemsSource绑定了ViewModel中的某个ObservableCollection< T >集合,但在Model中调用时尝试访问这个控件,并修改它的ItemsSource。如下面的写法会报错:

<ListBox x:Name="designViewLB" ItemsSource="{Binding DesignViewList}">
shellWindow.designViewLB.Items.Add(new DesignViewItem() {
    // ... 设置属性
});

解决:因为前台控件的ItemsSource已被绑定到某个集合中,所以不能再直接操作这个控件的ItemsSource,而应转为操作这个控件ItemsSource所绑定的集合。改为如下写法即可:

shellViewModel.DesignViewList.Add(new DesignViewItem()
{
    // ... 设置属性
});
好的,关于WPF运行动态切换ItemsControl.ItemsPanel的问题,我可以为您提供以下思路: 1. 创建多个ItemsPanel,例如:StackPanel、WrapPanel、UniformGrid等。 2. 在XAML文件中,使用Resources定义这些ItemsPanel。 3. 创建一个ItemsControl,并在ItemsControl使用DataTemplate来定义Item的样式。 4. 在ItemsControl中,使用一个默认的ItemsPanel。 5. 在运行,根据需要动态切换ItemsControlItemsPanel。 6. 在切换,先从ItemsControl的Resources中获取要切换的ItemsPanel,然后将其设置为ItemsControlItemsPanel。 以下是一个简单的示例代码: ```xml <!--MainWindow.xaml--> <Window> <Window.Resources> <ItemsPanelTemplate x:Key="StackPanelTemplate"> <StackPanel/> </ItemsPanelTemplate> <ItemsPanelTemplate x:Key="WrapPanelTemplate"> <WrapPanel/> </ItemsPanelTemplate> <ItemsPanelTemplate x:Key="UniformGridTemplate"> <UniformGrid/> </ItemsPanelTemplate> </Window.Resources> <StackPanel> <StackPanel Orientation="Horizontal"> <Button Content="StackPanel" Click="StackPanel_Click"/> <Button Content="WrapPanel" Click="WrapPanel_Click"/> <Button Content="UniformGrid" Click="UniformGrid_Click"/> </StackPanel> <ItemsControl ItemsSource="{Binding Items}"> <ItemsControl.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding}"/> </DataTemplate> </ItemsControl.ItemTemplate> <!--默认的ItemsPanel--> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <StackPanel/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> </ItemsControl> </StackPanel> </Window> ``` ```csharp //MainWindow.xaml.cs public partial class MainWindow : Window { public ObservableCollection<string> Items { get; set; } public MainWindow() { InitializeComponent(); Items = new ObservableCollection<string>() { "Item 1", "Item 2", "Item 3" }; DataContext = this; } private void StackPanel_Click(object sender, RoutedEventArgs e) { ItemsControl.ItemsPanel = (ItemsPanelTemplate)Resources["StackPanelTemplate"]; } private void WrapPanel_Click(object sender, RoutedEventArgs e) { ItemsControl.ItemsPanel = (ItemsPanelTemplate)Resources["WrapPanelTemplate"]; } private void UniformGrid_Click(object sender, RoutedEventArgs e) { ItemsControl.ItemsPanel = (ItemsPanelTemplate)Resources["UniformGridTemplate"]; } } ``` 在这个示例中,我们创建了三个不同的ItemsPanel,分别是StackPanel、WrapPanel和UniformGrid,然后在XAML文件中使用Resources定义它们。在MainWindow中,我们创建了一个ItemsControl,并在ItemsControl使用DataTemplate来定义Item的样式,同设置了一个默认的ItemsPanel。 在运行,我们通过点击按钮来动态切换ItemsControlItemsPanel。在每个按钮的Click事件中,我们从MainWindow的Resources中获取要切换的ItemsPanel,然后将其设置为ItemsControlItemsPanel。 希望以上思路能够对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值