Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.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# 如何删除列表框中的选定模板_C#_Wpf_Listbox_Itemsource - Fatal编程技术网

C# 如何删除列表框中的选定模板

C# 如何删除列表框中的选定模板,c#,wpf,listbox,itemsource,C#,Wpf,Listbox,Itemsource,我有一个列表框,其中有一个绑定到xml文件中任务的数据模板,我想在单击按钮时删除所选模板,但它会引发一个异常“当ItemsSource正在使用时,操作无效。请改为使用ItemsControl.ItemsSource访问和修改元素。” 下面是xaml的代码 <TabItem> <Canvas Height="700" Width="850"> <Canvas.Resources> <

我有一个列表框,其中有一个绑定到xml文件中任务的数据模板,我想在单击按钮时删除所选模板,但它会引发一个异常“当ItemsSource正在使用时,操作无效。请改为使用ItemsControl.ItemsSource访问和修改元素。”

下面是xaml的代码

 <TabItem>
        <Canvas Height="700" Width="850">
            <Canvas.Resources>
                <XmlDataProvider x:Key="Tasks" XPath="tasks"
       Source="http://store.tymesheet.com/templates/Graphic-Designer.xml"/>
                <DataTemplate x:Key="tasktemplate1">
                    <Canvas Height="50" Width="850" >
                        <Label Content="{Binding XPath=name}" Height="30"
                   Width="170" Canvas.Top="10" Canvas.Left="150" 
                   Background="LightGray"/>
                        <TextBox Height="30" Width="120" Canvas.Top="10"
                     Canvas.Left="370" Background="AliceBlue"/>
                        <Label  Canvas.Left="500" Canvas.Top="10">$</Label>
                        <Button Tag="{Binding}" Click="deletebuttonclick" 
                    Canvas.Top="12" Height="10" Width="30"
                    Canvas.Left="600"/>
                    </Canvas>
                </DataTemplate>
            </Canvas.Resources>
            <ListBox ItemTemplate="{StaticResource tasktemplate1}"
      ItemsSource="{Binding Path=ChildNodes, Source={StaticResource Tasks}}" 
      x:Name="tasklistbox" Height="700" Width="850"/>
            <Label Canvas.Top="-18" Canvas.Left="185">Select Task</Label>
            <Label Canvas.Top="-18" Canvas.Left="377" RenderTransformOrigin="0.58,0.462">Enter Bill Rates</Label>
            <Button Canvas.Left="39" Canvas.Top="575" Width="139">Click to add the task</Button>
        </Canvas>
    </TabItem>

我错在哪里?help.thanx.

您显示的错误是不言自明的:

ItemsSource
正在使用时,操作无效。改为使用
ItemsControl.ItemsSource
访问和修改元素

显然,您应该知道,您使用了
列表框上的
ItemsSource
属性:

<ListBox ItemTemplate="{StaticResource tasktemplate1}"
    ItemsSource="{Binding Path=ChildNodes, Source={StaticResource Tasks}}" 
    x:Name="tasklistbox" Height="700" Width="850" />
试着按照上面说的去做:

private void deletebuttonclick(object sender,RoutedEventArgs e)
{
    tasklistbox.Items.Remove(tasklistbox.SelectedItem);    
}
private void deletebuttonclick(object sender,RoutedEventArgs e)
{
    tasklistbox.ItemsSource = null; 
}
或者更好:

private void deletebuttonclick(object sender,RoutedEventArgs e)
{
    ChildNodes = null; 
}

更新>>>

因此,我们似乎又有了一个新用户,他问了一件事,而当我们给他一件事的时候,他又问了另一件事,连一句“谢谢”都没有。。。羞愧。。。真可惜

首先,需要将数据绑定到
列表框。选择editem
属性,以便知道选择了哪个项:

<ListBox ItemTemplate="{StaticResource tasktemplate1}" SelectedItem="{Binding Item}"
    ItemsSource="{Binding Path=ChildNodes, Source={StaticResource Tasks}}" 
    x:Name="tasklistbox" Height="700" Width="850" />

您显示的错误不言自明:

ItemsSource
正在使用时,操作无效。改为使用
ItemsControl.ItemsSource
访问和修改元素

显然,您应该知道,您使用了
列表框上的
ItemsSource
属性:

<ListBox ItemTemplate="{StaticResource tasktemplate1}"
    ItemsSource="{Binding Path=ChildNodes, Source={StaticResource Tasks}}" 
    x:Name="tasklistbox" Height="700" Width="850" />
试着按照上面说的去做:

private void deletebuttonclick(object sender,RoutedEventArgs e)
{
    tasklistbox.Items.Remove(tasklistbox.SelectedItem);    
}
private void deletebuttonclick(object sender,RoutedEventArgs e)
{
    tasklistbox.ItemsSource = null; 
}
或者更好:

private void deletebuttonclick(object sender,RoutedEventArgs e)
{
    ChildNodes = null; 
}

更新>>>

因此,我们似乎又有了一个新用户,他问了一件事,而当我们给他一件事的时候,他又问了另一件事,连一句“谢谢”都没有。。。羞愧。。。真可惜

首先,需要将数据绑定到
列表框。选择editem
属性,以便知道选择了哪个项:

<ListBox ItemTemplate="{StaticResource tasktemplate1}" SelectedItem="{Binding Item}"
    ItemsSource="{Binding Path=ChildNodes, Source={StaticResource Tasks}}" 
    x:Name="tasklistbox" Height="700" Width="850" />

您显示的错误不言自明:

ItemsSource
正在使用时,操作无效。改为使用
ItemsControl.ItemsSource
访问和修改元素

显然,您应该知道,您使用了
列表框上的
ItemsSource
属性:

<ListBox ItemTemplate="{StaticResource tasktemplate1}"
    ItemsSource="{Binding Path=ChildNodes, Source={StaticResource Tasks}}" 
    x:Name="tasklistbox" Height="700" Width="850" />
试着按照上面说的去做:

private void deletebuttonclick(object sender,RoutedEventArgs e)
{
    tasklistbox.Items.Remove(tasklistbox.SelectedItem);    
}
private void deletebuttonclick(object sender,RoutedEventArgs e)
{
    tasklistbox.ItemsSource = null; 
}
或者更好:

private void deletebuttonclick(object sender,RoutedEventArgs e)
{
    ChildNodes = null; 
}

更新>>>

因此,我们似乎又有了一个新用户,他问了一件事,而当我们给他一件事的时候,他又问了另一件事,连一句“谢谢”都没有。。。羞愧。。。真可惜

首先,需要将数据绑定到
列表框。选择editem
属性,以便知道选择了哪个项:

<ListBox ItemTemplate="{StaticResource tasktemplate1}" SelectedItem="{Binding Item}"
    ItemsSource="{Binding Path=ChildNodes, Source={StaticResource Tasks}}" 
    x:Name="tasklistbox" Height="700" Width="850" />

您显示的错误不言自明:

ItemsSource
正在使用时,操作无效。改为使用
ItemsControl.ItemsSource
访问和修改元素

显然,您应该知道,您使用了
列表框上的
ItemsSource
属性:

<ListBox ItemTemplate="{StaticResource tasktemplate1}"
    ItemsSource="{Binding Path=ChildNodes, Source={StaticResource Tasks}}" 
    x:Name="tasklistbox" Height="700" Width="850" />
试着按照上面说的去做:

private void deletebuttonclick(object sender,RoutedEventArgs e)
{
    tasklistbox.Items.Remove(tasklistbox.SelectedItem);    
}
private void deletebuttonclick(object sender,RoutedEventArgs e)
{
    tasklistbox.ItemsSource = null; 
}
或者更好:

private void deletebuttonclick(object sender,RoutedEventArgs e)
{
    ChildNodes = null; 
}

更新>>>

因此,我们似乎又有了一个新用户,他问了一件事,而当我们给他一件事的时候,他又问了另一件事,连一句“谢谢”都没有。。。羞愧。。。真可惜

首先,需要将数据绑定到
列表框。选择editem
属性,以便知道选择了哪个项:

<ListBox ItemTemplate="{StaticResource tasktemplate1}" SelectedItem="{Binding Item}"
    ItemsSource="{Binding Path=ChildNodes, Source={StaticResource Tasks}}" 
    x:Name="tasklistbox" Height="700" Width="850" />

如果您想删除该项,那么我建议创建XmlNode的
ObservableCollection
,并将ItemsSource与之绑定。我建议使用ObservaleCollection,因为它实现了INotifyCollectionChanged,所以每当更新列表时,目标(在您的情况下是ListBox)都会自动更新


代码隐藏中 (添加System.Collections.ObjectModel命名空间以使用
ObservableCollection

public主窗口()
{
初始化组件();
XmlDocument doc=新的XmlDocument();
文件加载(“http://store.tymesheet.com/templates/Software-Developer.xml");
var taskList=doc.ChildNodes.OfType()
.Where(node=>node.Name==“任务”)
.SelectMany(node=>node.ChildNodes.OfType());
任务=新的ObservableCollection(任务列表);
this.DataContext=this;
}
公共ObservableCollection任务{get;set;}
private void delete按钮单击(对象发送方,RoutedEventArgs e)
{
XmlNode selectedNode=((按钮)sender).DataContext作为XmlNode;
任务。删除(selectedNode);
}
当然,您还需要更新XAML

<ListBox ItemsSource="{Binding Tasks}"
         ItemTemplate="{StaticResource tasktemplate1}"
         x:Name="listBox" Height="700" Width="850"/>

如果您想删除该项,那么我建议创建XmlNode的
ObservableCollection
,并将ItemsSource与之绑定。我建议使用ObservaleCollection,因为它实现了INotifyCollectionChanged,所以每当更新列表时,目标(在您的情况下是ListBox)都会自动更新


代码隐藏中 (添加System.Collections.ObjectModel命名空间以使用
ObservableCollection

public主窗口()
{
初始化组件();
XmlDocument doc=新的XmlDocument();
文件加载(“http://store.tymesheet.com/templates/Software-Developer.xml");
var taskList=doc.ChildNodes.OfType()
.Where(node=>node.Name==“任务”)
.SelectMany(node=>node.ChildNodes.OfType());
任务=新的ObservableCollection(任务列表);
this.DataContext=this;
}
公共ObservableCollection任务{get;set;}
private void delete按钮单击(对象发送方,RoutedEventArgs e)
{
XmlNode selectedNode=((按钮)sender).DataContext作为XmlNode;
任务。删除(selectedNode);
}
当然,您还需要更新XAML

<ListBox ItemsSource="{Binding Tasks}"
         ItemTemplate="{StaticResource tasktemplate1}"
         x:Name="listBox" Height="700" Width="850"/>

如果您想删除该项,那么我建议创建XmlNode的
ObservableCollection
,并将ItemsSource与之绑定。我建议使用ObservableCollection,因为它实现了INotifyCollectionChange