Reputation: 171
i have ControlTemplate
, which is used as a resource inside a ListBox
, i am trying to delete the template i.e remove it totally from ListBox
on a Button
click
here is the code for template
<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>
here is the code for 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>
and the code behind is for the Button
click
private void deletebuttonclick(object sender,RoutedEventArgs e)
{
var r=listBox.FindResource("tasktemplate1");
listBox.Items.Remove(r);
}
where am i going wrong,help needed,thanx.
Upvotes: 0
Views: 238
Reputation: 81253
ControlTemplate is just visual representation of control that how it will look like.
So, you need to remove the item (ListBoxItem
) from the Items collection and not Template. Since templated control is removed, template automatically will be removed.
private void deletebuttonclick(object sender,RoutedEventArgs e)
{
listBox.Items.RemoveAt(0);
// listBox.Items.Clear(); OR in case want to clear all listBoxItems, use Clear
}
Upvotes: 1