我已经在列表框中为我的ListBoxItem创建了一个控件模板,并且每个ListBoxItem都由contentpresenter和一个图像组成。通过点击列表框中的特定元素来查找列表框
我对你的问题是......我怎么知道,当我点击列表框中的图像时,我点击了哪个列表框。
<Style x:Key="ListBoxItemWithDelete" TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="Border" Padding="2" SnapsToDevicePixels="true">
<Grid>
<ContentPresenter VerticalAlignment="Center" />
<Image Name="ImageListItemDelete" Source="../Resources/Images/actions-delete-big-1.png" Width="20" Style="{StaticResource MenuItemIcon}" HorizontalAlignment="Right"
MouseLeftButtonUp="ImageListItemDelete_MouseLeftButtonUp"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="Border" Property="Background" Value="{StaticResource SelectedBackgroundBrush}"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
private void ImageListItemDelete_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
//Object sender is my Image i Clicked.
if (ListBoxName.SelectedItem != null)
{
ListBoxName.Items.Remove(ListBoxName.SelectedItem);
}
}
我想用包含这个图像的列表框替换ListBoxName,我点击了,现在“ListBoxName”是硬编码的。
我知道如何通过listboxitems找到他们的内容模板,但我不知道如何以相反的方式工作。 :/
谢谢..这正是我正在寻找的。我知道另一个通过子元素向下穿过的人。 –