• WPF 重写ListBox(透明效果)


    
    
    <UserControl d:DesignHeight="460" d:DesignWidth="300" 
                 x:Name="UCcontrol">
        <UserControl.Resources>
            <!-- ScrollViewer -->
            <Style x:Key="ScrollBarThumb" TargetType="Thumb">
                <Setter Property="OverridesDefaultStyle" Value="true" />
                <Setter Property="BorderBrush" Value="Gray" />
                <Setter Property="IsTabStop" Value="false" />
                <Setter Property="Opacity" Value="1" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Thumb">
                            <Rectangle x:Name="recThumb"  Fill="{TemplateBinding BorderBrush}" RadiusX="3"  RadiusY="3" />
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsDragging" Value="True">
                                    <Setter Property="Fill" TargetName="recThumb" Value="#606060" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
            <Style x:Key="VerticalScrollBarPageButton"
                   TargetType="RepeatButton">
                <Setter Property="OverridesDefaultStyle" Value="true" />
                <Setter Property="Background" Value="Transparent" />
                <Setter Property="Focusable" Value="false" />
                <Setter Property="IsTabStop" Value="false" />
                <Setter Property="Opacity" Value="0" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="RepeatButton">
                            <Rectangle Fill="{TemplateBinding Background}" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" />
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
            <Style x:Key="HorizontalScrollBarPageButton"
                   TargetType="RepeatButton">
                <Setter Property="OverridesDefaultStyle" Value="true" />
                <Setter Property="Background" Value="Transparent" />
                <Setter Property="Focusable" Value="false" />
                <Setter Property="IsTabStop" Value="false" />
                <Setter Property="Opacity" Value="0" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="RepeatButton">
                            <Rectangle Fill="{TemplateBinding Background}" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" />
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
            <Style x:Key="for_scrollbar"
                   TargetType="ScrollBar">
                <Setter Property="Stylus.IsPressAndHoldEnabled" Value="false" />
                <Setter Property="Stylus.IsFlicksEnabled" Value="false" />
                <Setter Property="Background" Value="Transparent" />
                <Setter Property="Margin" Value="0,1,0,6" />
                <Setter Property="Width" Value="10" />
                <Setter Property="MinWidth" Value="10" />
                <Setter Property="Opacity" Value="0" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ScrollBar">
                            <Grid x:Name="Bg" SnapsToDevicePixels="true">
                                <Track x:Name="PART_Track"   IsEnabled="{TemplateBinding IsMouseOver}" IsDirectionReversed="true">
                                    <Track.DecreaseRepeatButton>
                                        <RepeatButton Style="{StaticResource VerticalScrollBarPageButton}" Command="{x:Static ScrollBar.PageUpCommand}" />
                                    </Track.DecreaseRepeatButton>
                                    <Track.IncreaseRepeatButton>
                                        <RepeatButton Style="{StaticResource VerticalScrollBarPageButton}" Command="{x:Static ScrollBar.PageDownCommand}" />
                                    </Track.IncreaseRepeatButton>
                                    <Track.Thumb>
                                        <Thumb Style="{StaticResource ScrollBarThumb}" />
                                    </Track.Thumb>
                                </Track>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <Trigger Property="Orientation" Value="Horizontal">
                        <Setter Property="Background" Value="Transparent" />
                        <Setter Property="Margin" Value="1,0,6,0" />
                        <Setter Property="Height" Value="10" />
                        <Setter Property="MinHeight" Value="10" />
                        <Setter Property="Width" Value="Auto" />
                        <Setter Property="Opacity" Value="0" />
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="ScrollBar">
                                    <Grid x:Name="Bg" SnapsToDevicePixels="true">
                                        <Track x:Name="PART_Track" IsEnabled="{TemplateBinding IsMouseOver}">
                                            <Track.DecreaseRepeatButton>
                                                <RepeatButton Style="{StaticResource HorizontalScrollBarPageButton}" Command="{x:Static ScrollBar.PageLeftCommand}" />
                                            </Track.DecreaseRepeatButton>
                                            <Track.IncreaseRepeatButton>
                                                <RepeatButton Style="{StaticResource HorizontalScrollBarPageButton}"  Command="{x:Static ScrollBar.PageRightCommand}" />
                                            </Track.IncreaseRepeatButton>
                                            <Track.Thumb>
                                                <Thumb Style="{StaticResource ScrollBarThumb}" />
                                            </Track.Thumb>
                                        </Track>
                                    </Grid>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                </Style.Triggers>
            </Style>
            <Style TargetType="ScrollViewer">
                <Setter Property="BorderBrush" Value="Black" />
                <Setter Property="BorderThickness"  Value="0" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ScrollViewer">
                            <Border BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                SnapsToDevicePixels="True">
                                <Grid Background="{TemplateBinding Background}">
                                    <ScrollContentPresenter
                                    Cursor="{TemplateBinding Cursor}"
                                    Margin="{TemplateBinding Padding}"
                                    ContentTemplate="{TemplateBinding ContentTemplate}" />
                                    <ScrollBar x:Name="PART_VerticalScrollBar"
                                           Opacity="1"
                                           HorizontalAlignment="Right"
                                           Maximum="{TemplateBinding ScrollableHeight}"
                                           Orientation="Vertical"
                                           Style="{StaticResource for_scrollbar}"
                                           ViewportSize="{TemplateBinding ViewportHeight}"
                                           Value="{TemplateBinding VerticalOffset}"
                                           Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"
                                           Width="10" />
                                    <ScrollBar x:Name="PART_HorizontalScrollBar"
                                           Opacity="1"
                                           Maximum="{TemplateBinding ScrollableWidth}"
                                           Orientation="Horizontal"
                                           Style="{StaticResource for_scrollbar}"
                                           VerticalAlignment="Bottom"
                                           Value="{TemplateBinding HorizontalOffset}"
                                           ViewportSize="{TemplateBinding ViewportWidth}"
                                           Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"
                                           Height="10" />
                                </Grid>
                            </Border>
                            <ControlTemplate.Triggers>
                                <EventTrigger RoutedEvent="ScrollChanged">
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <DoubleAnimation
                                            Storyboard.TargetName="PART_VerticalScrollBar"
                                            Storyboard.TargetProperty="Opacity"
                                            To="1"
                                            Duration="0:0:1" />
                                            <DoubleAnimation
                                            Storyboard.TargetName="PART_VerticalScrollBar"
                                            Storyboard.TargetProperty="Opacity"
                                            To="0"
                                            Duration="0:0:1"
                                            BeginTime="0:0:1" />
                                        </Storyboard>
                                    </BeginStoryboard>
                                </EventTrigger>
                                <EventTrigger RoutedEvent="MouseEnter"
                                          SourceName="PART_VerticalScrollBar">
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <DoubleAnimation
                                            Storyboard.TargetName="PART_VerticalScrollBar"
                                            Storyboard.TargetProperty="Opacity"
                                            To="1"
                                            Duration="0:0:1" />
                                        </Storyboard>
                                    </BeginStoryboard>
                                </EventTrigger>
                                <EventTrigger RoutedEvent="MouseLeave"
                                          SourceName="PART_VerticalScrollBar">
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <DoubleAnimation
                                            Storyboard.TargetName="PART_VerticalScrollBar"
                                            Storyboard.TargetProperty="Opacity"
                                            To="0"
                                            Duration="0:0:1" />
                                        </Storyboard>
                                    </BeginStoryboard>
                                </EventTrigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
            <local:FontWeightConverter x:Key="FontWeightConverterStyle" />
            <local:ThicknessConverter x:Key="ThicknessConverterStyle" />
            <local:OpacityConverter x:Key="OpacityConverterStyle" />
            <!--item-->
            <DataTemplate x:Key="rect">
                <Grid Name="gd" Height="60">
                    <Border Name="border" 
                            ToolTip="{Binding BSM}" 
                            Background="#495A5A"
                            BorderBrush="White" 
                            BorderThickness="0,0,0,1" 
                            Opacity="{Binding Path=DM,Converter={StaticResource OpacityConverterStyle}}"
                            MouseLeftButtonDown="border_MouseLeftButtonDown">
                    </Border>
                    <StackPanel Orientation="Horizontal"
                                Margin="{Binding Path=DM,Converter={StaticResource ThicknessConverterStyle}}">
                        <TextBlock FontWeight="{Binding Path=DM,Converter={StaticResource FontWeightConverterStyle}}"
                                   HorizontalAlignment="Center"
                                   VerticalAlignment="Center"
                                   ToolTip="{Binding MC,Mode=TwoWay}" 
                                   Text="{Binding MC,Mode=TwoWay}" 
                                   FontSize="24"
                                   Foreground="White"
                                   Margin="5,0" />
                    </StackPanel>
                </Grid>
                <DataTemplate.Triggers>
                    <!--鼠标移入-->
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsMouseOver" Value="True"/>
                        </MultiTrigger.Conditions>
                        <Setter TargetName="gd" Property="Background"  Value="Black"/>
                        <Setter TargetName="gd" Property="Opacity" Value="0.5"/>
                    </MultiTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>
            <!--item 方式-->
            <ItemsPanelTemplate x:Key="items">
                <StackPanel Orientation="Vertical" VerticalAlignment="Top" 
                            HorizontalAlignment="Left" />
            </ItemsPanelTemplate>
        </UserControl.Resources>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="50"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
    
            <Border Panel.ZIndex="0" 
                    BorderThickness="2"
                    BorderBrush="White"
                    Width="{Binding ElementName=UCcontrol,Path=Width}" 
                    Height="{Binding ElementName=UCcontrol,Path=Height}"
                    CornerRadius="10" Opacity="0.5" Grid.RowSpan="2">
                <Border.Background>
                    <LinearGradientBrush EndPoint="0.5,1" 
                                         MappingMode="RelativeToBoundingBox" 
                                         StartPoint="0.5,0">
                        <GradientStop Color="#FF787D79"/>
                        <GradientStop Offset="1"/>
                    </LinearGradientBrush>
                </Border.Background>
            </Border>
    
            <TextBlock Text="行政区划" 
                       Margin="15,0,0,0"
                       FontSize="28"
                       Foreground="White"
                       HorizontalAlignment="Left"
                       VerticalAlignment="Bottom"
                       Grid.Row="0"
                       />
    
            <ListBox Name="Lbox"
                     Margin="10"
                     VerticalContentAlignment="Bottom"
                     VerticalAlignment="Bottom"
                     Panel.ZIndex="1"
                     BorderThickness="0"
                     ScrollViewer.HorizontalScrollBarVisibility="Hidden"
                     ScrollViewer.VerticalScrollBarVisibility="Auto"
                     ItemTemplate="{StaticResource ResourceKey=rect}"
                     ItemsPanel="{StaticResource ResourceKey=items}" 
                     Background="Transparent"
                     Grid.Row="1">
                <ListBox.Style>
                    <Style>
                        <Style.Resources>
                            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
                                     Color="Black" Opacity="0.8"/>
                        </Style.Resources>
                    </Style>
                </ListBox.Style>
                <ListBox.ItemContainerStyle>
                    <Style x:Name="s" TargetType="ListBoxItem">
                        <Setter Property="Width" Value="{Binding ElementName=UCcontrol,Path=Width}"/>
                        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                        <Setter Property="HorizontalAlignment" Value="Stretch"/>
                        <Setter Property="VerticalContentAlignment" Value="Stretch"/>
                        <Setter Property="VerticalAlignment" Value="Stretch"/>
                    </Style>
                </ListBox.ItemContainerStyle>
            </ListBox>
        </Grid>
    </UserControl>
    
    
    
     
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.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 Kingo.Land.JCJG.Controls
    {
        /// <summary>
        /// UCListBox.xaml 的交互逻辑
        /// </summary>
        public partial class UCListBox : UserControl
        {
            private string OracleConn = System.Configuration.ConfigurationManager.AppSettings["OracleConn"];
    
            public XZQModelList XZQ = new XZQModelList();
            public UCListBox()
            {
                InitializeComponent();
                Init();
            }
    
            public void Init()
            {
                DataTable dt0 = GetData("select distinct CMC,CDM from tb_hcqy");
                foreach (DataRow item0 in dt0.Rows)
                {
                    XZQ.Add(new Controls.XZQModel
                    {
                        MC = item0["CMC"].ToString(),
                        DM = item0["CDM"].ToString(),
                    });
                    DataTable dt1 = GetData("select distinct XMC,XDM from tb_hcqy where CMC='" + item0["CMC"].ToString() + "'");
                    foreach (DataRow item1 in dt1.Rows)
                    {
                        XZQ.Add(new Controls.XZQModel
                        {
                            MC = item1["XMC"].ToString(),
                            DM = item1["XDM"].ToString(),
                        });
                    }
                }
                //将P1数据绑定给listbox控件
                Lbox.ItemsSource = XZQ;
            }
    
    
            //选中行政区
            private void border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                var bor = (sender as Border).ToolTip;
            }
    
            public DataTable GetData(string sql)
            {
                Kingo.Common.DBOperator.IRDBHelper dbHelper = Kingo.Common.DBOperator.RDBFactory.CreateDbHelper(OracleConn, Kingo.Common.DBOperator.DatabaseType.Oracle);
                DataTable d = dbHelper.ExecuteDatatable("goodgoodstudy", sql, true);
                dbHelper.DisConnect();
                return d;
            }
        }
    
        public class XZQModelList : ObservableCollection<XZQModel>
        {
        }
    
        /// <summary>
        /// 用来存放原始影像表部分数据
        /// </summary>
        public class XZQModel
        {
            /// <summary>
            /// 行政区 标识码
            /// </summary>
            public string BSM { get; set; }
    
            /// <summary>
            /// 行政区名称
            /// </summary>
            public string MC { get; set; }
    
            /// <summary>
            /// 行政区父级Id
            /// </summary>
            public string DM { get; set; }
    
        }
    
        /// <summary>
        /// 定义转换器,将ListBox中市级的变为加粗字体
        /// </summary>
        [ValueConversion(typeof(string), typeof(FontWeight))]
        public class FontWeightConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                if (value.ToString().Length != 4)
                    return FontWeights.Normal;
                else
                    return FontWeights.Black;
            }
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    
        /// <summary>
        /// 转换器 位置
        /// </summary>
        [ValueConversion(typeof(string), typeof(Thickness))]
        public class ThicknessConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                if (value.ToString().Length == 4)
                    return new Thickness(10, 5, 0, 5);
                else
                    return new Thickness(30, 5, 0, 5);
            }
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    
        /// <summary>
        /// 转换器 透明度
        /// </summary>
        [ValueConversion(typeof(string), typeof(double))]
        public class OpacityConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                if (value.ToString().Length == 4)
                    return 0.2;
                else
                    return 0.1;
            }
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    }
     <UC:UCListBox HorizontalAlignment="Left" Height="500" Width="600"/>

    效果:

     

  • 相关阅读:
    记录一下思源服务端的安装过程
    在Github上键政,然后被封号
    使用Aspnet_regiis加密web.config binzi
    策略模式
    Android Ril 分析
    20192422李俊洁 汇编语言程序设计1~4章学习笔记
    阿里云服务器如何配置开放端口
    关于DIFF插件的使用
    Python3 web开发中几种密码加密方式
    中间件安装——用Docker在CentOS7部署MongoDB服务
  • 原文地址:https://www.cnblogs.com/SeNaiTes/p/10426819.html
Copyright © 2020-2023  润新知