【问题标题】:How to create a WPF ListBoxItem with check mark?如何创建带有复选标记的 WPF ListBoxItem?
【发布时间】:2019-10-11 02:26:44
【问题描述】:

我一直在寻找如何构建一个带有复选标记的列表项(如果选中)。

查看不同的资源,似乎有很多代码解决方案,bot no true XAML 只有一个。

这就是我试图达到的目标:

欢迎补充。

本着Answer-your-own (Stackoverflow blog)的精神

【问题讨论】:

    标签: wpf xaml listbox microsoft-metro listboxitem


    【解决方案1】:

    所以这是我经过数小时的研究后实际得到的结果。 如前所述,欢迎任何补充。

    复选标记是:

    U+2714 (10004)  ✔   HEAVY CHECK MARK    Dingbats (2700–27BF)
    

    样式代码:

    • 基本上为任何项目创建一个包装器,设置基本属性,并在项目选择时更改它们,以StaticResourcecheckmarkItem 的形式提供
    <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>
    

    实施:

    • 它只是一个常规的stackpanel排列,所有样式都通过引用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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-17
      • 1970-01-01
      • 2011-09-11
      • 2021-05-03
      • 1970-01-01
      • 2010-09-12
      • 2012-04-10
      • 2016-12-26
      相关资源
      最近更新 更多