Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从ItemControl中删除项_C#_Wpf_Xaml - Fatal编程技术网

C# 从ItemControl中删除项

C# 从ItemControl中删除项,c#,wpf,xaml,C#,Wpf,Xaml,我的类是viewmodel类的ObservableCollection,我在xaml中设置Itemcontrol的itemsource,如下所示 <ItemsControl ItemsSource="{Binding ConditionItems}"> <ItemsControl.ItemTemplate> <DataTemplate> <Ex

我的类是viewmodel类的ObservableCollection,我在xaml中设置Itemcontrol的itemsource,如下所示

        <ItemsControl ItemsSource="{Binding ConditionItems}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Expander Background="#FFD0D7EB">
                        <StackPanel>
                                <Button Content="Delete" HorizontalAlignment="Right" Width="180" Margin="0,0,12,10" Command="{Binding DeleteItem}" CommandParameter="{Binding}">
                                </Button>                             </StackPanel>
                    </Expander>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </StackPanel>
我在xaml中是否做错了什么?

ItemsControl是使用{Binding ConditionItems}绑定的,因此它希望DeleteItem命令位于该列表的子项中。我想情况并非如此,ViewModel上存在DeleteItem


例如,可以绑定到窗口的DataContext,在这里可以找到DeleteItem命令。或者创建一个代理元素。

我找到了它。我的xaml应该是

<Button Content="Delete" Command="{Binding DataContext.DeleteItem,
                          RelativeSource={RelativeSource FindAncestor,
                                          AncestorType={x:Type ItemsControl}}}" CommandParameter="{Binding}">
                                </Button>

DeleteItem在哪里定义?它是父视图模型还是项目视图模型的一部分?@dkozl我尝试将其放置在viewmodel中。问题是,我的ObservableCollection是viewmodel列表,因此即使调用了该函数,我也无法将其放置在那里。我试着把它放在xaml.cs文件中,该文件有我的ObservableCollection列表。在这两种情况下,如果我将Delete按钮放在ItemControl之外,它都不起作用,那么用户如何知道他正在删除哪个项目呢?不起作用。这就是为什么我没有提议移动它。这确实是我的意思。但是没有编辑器来编写代码。
<Button Content="Delete" Command="{Binding DataContext.DeleteItem,
                          RelativeSource={RelativeSource FindAncestor,
                                          AncestorType={x:Type ItemsControl}}}" CommandParameter="{Binding}">
                                </Button>