通过WPF自定义ListBox控件实现ListBoxItem中开关及删除功能

通过WPF自定义ListBox控件实现类似CAD图层关闭与删除功能


前言

提示:这里可以添加本文要记录的大概内容:
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。


提示:以下是本篇文章正文内容,下面案例可供参考

一、程序界面设计

ListBox控件设计如下所示,主要用到了TabControl、ListBox以及Button控件。
在这里插入图片描述

二、代码部分

2.1前端代码

<Window
    x:Class="ListBoxDemo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:ListBoxDemo"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="200"
    Height="450"
    mc:Ignorable="d">
    <Window.Resources>
        <Style x:Key="FocusVisual">
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Rectangle
                            Margin="2"
                            SnapsToDevicePixels="true"
                            Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"
                            StrokeDashArray="1 2"
                            StrokeThickness="1" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <SolidColorBrush x:Key="Item.MouseOver.Background" Color="#1F26A0DA" />
        <SolidColorBrush x:Key="Item.MouseOver.Border" Color="#a826A0Da" />
        <SolidColorBrush x:Key="Item.SelectedInactive.Background" Color="#3DDADADA" />
        <SolidColorBrush x:Key="Item.SelectedInactive.Border" Color="#FFDADADA" />
        <SolidColorBrush x:Key="Item.SelectedActive.Background" Color="#3D26A0DA" />
        <SolidColorBrush x:Key="Item.SelectedActive.Border" Color="#FF26A0DA" />
        <Style x:Key="ListBoxItemStyle1" TargetType="{x:Type ListBoxItem}">
            <Setter Property="SnapsToDevicePixels" Value="True" />
            <Setter Property="Padding" Value="1,0" />
            <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
            <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
            <Setter Property="Background" Value="Transparent" />
            <Setter Property="BorderBrush" Value="Transparent" />
            <Setter Property="BorderThickness" Value="0" />
            <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                        <Border
                            x:Name="Bd"
                            Grid.Column="1"
                            Padding="{TemplateBinding Padding}"
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            SnapsToDevicePixels="true">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="30" />
                                    <ColumnDefinition Width="1" />
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="1" />
                                    <ColumnDefinition Width="30" />
                                </Grid.ColumnDefinitions>
                                <Button
                                    x:Name="btn_DisplaySwitch"
                                    Grid.Column="0"
                                    Background="Transparent"
                                    BorderBrush="Transparent"
                                    Click="Btn_DisplaySwitch"
                                    Content="O"
                                    Foreground="Black" />
                                <TextBlock Grid.Column="1" Background="Gray" />
                                <ContentPresenter
                                    Grid.Column="2"
                                    Margin="10,0,0,0"
                                    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                    VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                                <TextBlock Grid.Column="3" Background="Gray" />
                                <Button
                                    x:Name="btn_DeleteLayer"
                                    Grid.Column="4"
                                    Background="Transparent"
                                    BorderBrush="Transparent"
                                    Click="Btn_DeleteLayer_Click"
                                    Content="X" />
                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsMouseOver" Value="True" />
                                </MultiTrigger.Conditions>
                                <Setter TargetName="Bd" Property="Background" Value="{StaticResource Item.MouseOver.Background}" />
                                <Setter TargetName="Bd" Property="BorderBrush" Value="{StaticResource Item.MouseOver.Border}" />
                            </MultiTrigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="Selector.IsSelectionActive" Value="False" />
                                    <Condition Property="IsSelected" Value="True" />
                                </MultiTrigger.Conditions>
                                <Setter TargetName="Bd" Property="Background" Value="{StaticResource Item.SelectedInactive.Background}" />
                                <Setter TargetName="Bd" Property="BorderBrush" Value="{StaticResource Item.SelectedInactive.Border}" />
                            </MultiTrigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="Selector.IsSelectionActive" Value="True" />
                                    <Condition Property="IsSelected" Value="True" />
                                </MultiTrigger.Conditions>
                                <Setter TargetName="Bd" Property="Background" Value="{StaticResource Item.SelectedActive.Background}" />
                                <Setter TargetName="Bd" Property="BorderBrush" Value="{StaticResource Item.SelectedActive.Border}" />
                            </MultiTrigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter TargetName="Bd" Property="TextElement.Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <SolidColorBrush x:Key="ListBox.Static.Background" Color="#FFFFFFFF" />
        <SolidColorBrush x:Key="ListBox.Static.Border" Color="#FFABADB3" />
        <SolidColorBrush x:Key="ListBox.Disabled.Background" Color="#FFFFFFFF" />
        <SolidColorBrush x:Key="ListBox.Disabled.Border" Color="#FFD9D9D9" />
        <Style x:Key="ListBoxStyle1" TargetType="{x:Type ListBox}">
            <Setter Property="Background" Value="{StaticResource ListBox.Static.Background}" />
            <Setter Property="BorderBrush" Value="{StaticResource ListBox.Static.Border}" />
            <Setter Property="BorderThickness" Value="1" />
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
            <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
            <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
            <Setter Property="ScrollViewer.CanContentScroll" Value="true" />
            <Setter Property="ScrollViewer.PanningMode" Value="Both" />
            <Setter Property="Stylus.IsFlicksEnabled" Value="False" />
            <Setter Property="VerticalContentAlignment" Value="Center" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBox}">
                        <Border
                            x:Name="Bd"
                            Padding="1"
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            SnapsToDevicePixels="true">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="31" />
                                    <ColumnDefinition Width="1" />
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="1" />
                                    <ColumnDefinition Width="31" />
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="20" />
                                    <RowDefinition Height="1" />
                                    <RowDefinition Height="*" />
                                </Grid.RowDefinitions>
                                <TextBlock
                                    Grid.Row="0"
                                    Grid.Column="0"
                                    HorizontalAlignment="Center"
                                    VerticalAlignment="Center"
                                    Text="显示" />
                                <TextBlock
                                    Grid.Row="0"
                                    Grid.Column="1"
                                    Background="Gray" />
                                <TextBlock
                                    Grid.Row="0"
                                    Grid.Column="2"
                                    HorizontalAlignment="Center"
                                    VerticalAlignment="Center"
                                    Text="图层名称" />
                                <TextBlock
                                    Grid.Row="0"
                                    Grid.Column="3"
                                    Background="Gray" />
                                <TextBlock
                                    Grid.Row="0"
                                    Grid.Column="4"
                                    HorizontalAlignment="Center"
                                    VerticalAlignment="Center"
                                    Text="删除" />
                                <TextBlock
                                    Grid.Row="1"
                                    Grid.ColumnSpan="5"
                                    Background="Gray" />
                                <ScrollViewer
                                    Grid.Row="2"
                                    Grid.ColumnSpan="5"
                                    Padding="{TemplateBinding Padding}"
                                    Focusable="false"
                                    HorizontalScrollBarVisibility="Hidden"
                                    VerticalScrollBarVisibility="Hidden">
                                    <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                                </ScrollViewer>
                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter TargetName="Bd" Property="Background" Value="{StaticResource ListBox.Disabled.Background}" />
                                <Setter TargetName="Bd" Property="BorderBrush" Value="{StaticResource ListBox.Disabled.Border}" />
                            </Trigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsGrouping" Value="true" />
                                    <Condition Property="VirtualizingPanel.IsVirtualizingWhenGrouping" Value="false" />
                                </MultiTrigger.Conditions>
                                <Setter Property="ScrollViewer.CanContentScroll" Value="false" />
                            </MultiTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <StackPanel>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="25"/>
                <RowDefinition Height="25"/>
                <RowDefinition Height="25"/>
                <RowDefinition Height="25"/>
            </Grid.RowDefinitions>
            <TabControl x:Name="tabCtr" Grid.Row="0" >
                <TabItem Name="tab_Column" Width="30" Header="柱" IsSelected="True">
                    <ListBox
                            Name="lbox_Column"                            
                            BorderBrush="Gray"
                            Style="{DynamicResource ListBoxStyle1}">
                        <ListBoxItem Style="{DynamicResource ListBoxItemStyle1}">1</ListBoxItem>
                        <ListBoxItem Style="{DynamicResource ListBoxItemStyle1}">1</ListBoxItem>
                        <ListBoxItem Style="{DynamicResource ListBoxItemStyle1}">1</ListBoxItem>
                    </ListBox>
                </TabItem>
                <TabItem Name="tab_Bean" Width="30" Header="梁">
                    <ListBox
                            Name="lbox_Bean"                            
                            BorderBrush="Gray"
                            Style="{DynamicResource ListBoxStyle1}">
                        <ListBoxItem Style="{DynamicResource ListBoxItemStyle1}">1</ListBoxItem>
                        <ListBoxItem Style="{DynamicResource ListBoxItemStyle1}">1</ListBoxItem>
                        <ListBoxItem Style="{DynamicResource ListBoxItemStyle1}">1</ListBoxItem>
                    </ListBox>
                </TabItem>
                <TabItem Name="tab_Wall" Width="30" Header="墙" IsSelected="True">
                    <ListBox
                            Name="lbox_Wall"                            
                            BorderBrush="Gray"
                            Style="{DynamicResource ListBoxStyle1}">
                        <ListBoxItem Style="{DynamicResource ListBoxItemStyle1}">1</ListBoxItem>
                        <ListBoxItem Style="{DynamicResource ListBoxItemStyle1}">1</ListBoxItem>
                        <ListBoxItem Style="{DynamicResource ListBoxItemStyle1}">1</ListBoxItem>
                    </ListBox>
                </TabItem>
                <TabItem Name="tab_Palte" Width="30" Header="板" IsSelected="True">
                    <ListBox
                            Name="lbox_Plate"                            
                            BorderBrush="Gray"
                            Style="{DynamicResource ListBoxStyle1}">
                        <ListBoxItem Style="{DynamicResource ListBoxItemStyle1}">1</ListBoxItem>
                        <ListBoxItem Style="{DynamicResource ListBoxItemStyle1}">1</ListBoxItem>
                        <ListBoxItem Style="{DynamicResource ListBoxItemStyle1}">1</ListBoxItem>
                    </ListBox>
                </TabItem>
            </TabControl>
            <Button Grid.Row="1" 
                            Name="btn_AddLayer"                            
                            Click="Btn_AddLayer"
                            Content="添加图层" />            
        </Grid>
    </StackPanel>
</Window>

2.2后台代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ListBoxDemo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        
        private void Btn_DeleteLayer_Click(object sender, RoutedEventArgs e)
        {
            ListBoxItem item = (sender as Button).TemplatedParent as ListBoxItem;
            this.lbox_Bean.Items.Remove(item);
            //foreach (var item in this.lbox_Bean.Items)
            //{                
            //    str += ((ListBoxItem)item).Content.ToString()+"\n";
            //}
            //MessageBox.Show(str);
        }
        int count = 0;
        private void Btn_AddLayer(object sender, RoutedEventArgs e)
        {
            ++count;
            string str = count.ToString();
            //TabItem tabItem = this.tabCtr.SelectedItem as TabItem;
            //ListBox lbox=tabItem.Content as ListBox;
            ListBox lbox = this.tabCtr.SelectedContent as ListBox;
            ListBoxItem item = new ListBoxItem();
            item.Content = str;
            item.Style = ((ListBoxItem)(lbox.Items[0])).Style;
            lbox.Items.Add(item);            
        }

        private void Btn_DisplaySwitch(object sender, RoutedEventArgs e)
        {
            ListBoxItem item = (sender as Button).TemplatedParent as ListBoxItem;
            string layerName = item.Content.ToString();
            if ((sender as Button).Content.ToString()=="O")
            {
                (sender as Button).Content = "▫";
                (sender as Button).Foreground = Brushes.Gray;
            }
            else
            {
                (sender as Button).Content = "O";
                (sender as Button).Foreground = Brushes.Black;
            }
        }
    }
}

总结

主要是通过控件模板来改变ListBox样式,实现类似AutoCad图层开关及删除的效果。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值