【发布时间】:2019-10-11 02:26:44
【问题描述】:
我一直在寻找如何构建一个带有复选标记的列表项(如果选中)。
查看不同的资源,似乎有很多代码解决方案,bot no true XAML 只有一个。
这就是我试图达到的目标:
欢迎补充。
【问题讨论】:
标签: wpf xaml listbox microsoft-metro listboxitem
我一直在寻找如何构建一个带有复选标记的列表项(如果选中)。
查看不同的资源,似乎有很多代码解决方案,bot no true XAML 只有一个。
这就是我试图达到的目标:
欢迎补充。
【问题讨论】:
标签: wpf xaml listbox microsoft-metro listboxitem
所以这是我经过数小时的研究后实际得到的结果。 如前所述,欢迎任何补充。
复选标记是:
U+2714 (10004) ✔ HEAVY CHECK MARK Dingbats (2700–27BF)
样式代码:
StaticResource 和checkmarkItem 的形式提供
<Window.Resources>
<Style x:Key="checkmarkItem" TargetType="ListBoxItem">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="Border" Background="Transparent" BorderThickness="5" BorderBrush="Transparent" Margin="0,1,0,1">
<Grid>
<TextBlock VerticalAlignment="Top" HorizontalAlignment="Right" Name="Marker" Visibility="Hidden" Background="#0078D7" Padding="5,0,0,5" Foreground="White">✔</TextBlock>
<ContentPresenter />
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="ListBoxItem.IsSelected" Value="true">
<Setter TargetName="Marker" Property="Visibility" Value="Visible" />
<Setter TargetName="Border" Property="BorderBrush" Value="#0078D7"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
实施:
ItemContainerStyle="{StaticResource checkmarkItem}"添加
<Grid>
<Label Grid.Row="0" FontSize="26">Software</Label>
<ListView Grid.Row="1" SelectionMode="Multiple" BorderBrush="Transparent" ItemContainerStyle="{StaticResource checkmarkItem}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Height="64" AutomationProperties.Name="{Binding Title}">
<Image Source="{Binding Icon}" Height="48" Width="48" VerticalAlignment="Center" Margin="5,0,20,0"/>
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<TextBlock Text="{Binding Title}" FontSize="16" TextWrapping="Wrap"/>
<TextBlock Text="{Binding Description}" TextWrapping="Wrap" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
【讨论】: