c# - ListBox selection only one item in wpf xaml
问题描述
I have a ListBox
in my WPF application. That ListBox
contain Image
control, TextBlock
etc. When user click on one item in ListBox
, I want to select only that Image
control. see the attached image. How can I do like this selection?
my code,
<ListBox Name="mediaListBox" BorderThickness="0" SelectionChanged="mediaListBox_SelectionChanged" ItemsSource="{Binding ThumbnailItems}" SnapsToDevicePixels="True" SelectedItem="{Binding SelectedItem}" Background="Transparent" Drop="ImagePanel_Drop" AllowDrop="true">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="IsSelected" Value="True"/>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ScrollContentPresenter}}}" >
</WrapPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<StackPanel Width="120">
<Grid>
<Image Height="90" Source="{Binding Path=Thumbnail}" Margin="0,8,8,0" MouseLeftButtonDown="Image_MouseLeftButtonDown"/>
<Button Visibility="{Binding IsVisible,Converter={StaticResource booleanVisibilityConverter}, FallbackValue=Hidden,UpdateSourceTrigger=PropertyChanged}" Style="{DynamicResource CloseButton}" HorizontalAlignment="Right" VerticalAlignment="Top" Command="{Binding ElementName=mediaListBox,Path=DataContext.RemoveGallaryItemCommand}" CommandParameter="{Binding SelectedItem}" Width="16" Height="16" Content="x"></Button>
</Grid>
<TextBlock TextWrapping="NoWrap" TextTrimming="CharacterEllipsis" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White" Text="{Binding Path=Name}" >
<TextBlock.ToolTip>
<ToolTip Visibility="{Binding RelativeSource={RelativeSource Self}, Path=PlacementTarget, Converter={StaticResource trimmedVisibilityConverter}}">
<ToolTip.Content>
<TextBlock Text="{Binding Name}"/>
</ToolTip.Content>
</ToolTip>
</TextBlock.ToolTip>
</TextBlock>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Currently,
解决方案
ControlTemplate
您可以为ListBoxItem
容器定义自定义,例如:
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
SnapsToDevicePixels="true">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="BorderBrush" Value="Green" />
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" Value="Green" />
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
推荐阅读
- algorithm - 当存在多个最优解时如何用匈牙利算法解决分配问题
- kotlin - KtLint 检查不再在本地工作 - 它在我的 docker 映像构建中工作
- python - 如何从列表中删除特定对象?
- node.js - 如何在 Angular 中读取“JSON”对象
- python - 使用模拟测试引发异常
- python - 基本 python 类和大型 if 语句
- javascript - 有没有一种有效的方法可以使用坐标按到地理位置的距离对对象数组进行排序?
- node.js - 如何在两个模式 mognose node.js 之间进行参考
- vba - Microsoft Access VBA 在计算中使用变量字段
- r - 在ggplot2中;在这种情况下,如何将 x 轴移动到顶部,然后使用 coord_flip () 旋转轴?