我有ControlTemplate
,用作ListBox
内的资源,我正在尝试删除该模板,即在ListBox
点击时Button
完全删除该模板
这是模板的代码
<ControlTemplate x:Key="tasktemplate1">
<Canvas Height="50" Width="850">
<Label Content="{Binding XPath=task[1]/name}" Height="30" Width="170" Canvas.Top="10" Canvas.Left="150" Background="LightGray">
</Label>
<TextBox Height="30" Width="120" Canvas.Top="10" Canvas.Left="370" Background="AliceBlue"></TextBox>
<Label Canvas.Left="500" Canvas.Top="10">$</Label>
<Button Click="deletebuttonclick" Canvas.Top="12" Height="10" Width="30" Canvas.Left="600" ></Button>
</Canvas>
</ControlTemplate>
这是ListBox
<TabItem>
<Canvas Height="700" Width="850">
<ListBox x:Name="listBox" Height="700" Width="850">
<ListBoxItem DataContext="{Binding Source={StaticResource TaskList}}" Template="{StaticResource tasktemplate1}"/>
</ListBox>
</Canvas>
</TabItem>
后面的代码是Button
点击
private void deletebuttonclick(object sender,RoutedEventArgs e)
{
var r=listBox.FindResource("tasktemplate1");
listBox.Items.Remove(r);
}
我哪里出错了,需要帮助,而不是。
答案 0 :(得分:1)
ControlTemplate只是控制的直观表示,它的外观如何。
因此,您需要从项目集合中删除项目( ListBoxItem
),而不是模板。由于移除了模板化控件,因此将自动删除模板。
private void deletebuttonclick(object sender,RoutedEventArgs e)
{
listBox.Items.RemoveAt(0);
// listBox.Items.Clear(); OR in case want to clear all listBoxItems, use Clear
}