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 != )
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 == )
return new Thickness(, , , );
else
return new Thickness(, , , );
}
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 == )
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"/>
效果:

WPF 重写ListBox(透明效果)的更多相关文章
- WPF中ListBox滚动时的缓动效果
原文:WPF中ListBox滚动时的缓动效果 上周工作中遇到的问题: 常规的ListBox在滚动时总是一格格的移动,感觉上很生硬. 所以想要实现类似Flash中的那种缓动的效果,使ListBox滚动时 ...
- 如何在WPF控件上应用简单的褪色透明效果?
原文 https://dailydotnettips.com/how-to-create-simple-faded-transparent-controls-in-wpf/ 使用OpacityMask ...
- WPF通过不透明蒙板切割显示子控件
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/Backspace110/article/ ...
- WPF ItemsControl ListBox ListView比较
在进行列表信息展示时,WPF中提供多种列表可供选择.这篇博客将对WPF ItemsControl, ListBox, ListView进行比较. 相同点: 1. 这三个控件都是列表型控件,可以进行列表 ...
- WPF 有趣的动画效果
WPF 有趣的动画效果 这一次我要呈上一个简单的文章,关于给你的WPF apps加入美丽的光线动画,可是我对动画这东西可能有点入迷了. 实际上.我对动画如此的入迷,以至 ...
- WPF Windows背景透明其中的文字保持不透明
原文:WPF Windows背景透明其中的文字保持不透明 版权声明:本文为博主原创,未经允许不得转载.交流.源码资料加群:161154103 https://blog.csdn.net/mpegfou ...
- Android课程---Android设置透明效果的三种方法(转)
1.使用Android系统自带的透明效果资源 <Button android:background="@android:color/transparent"/> ...
- Unity3D ShaderLab 使用alpha参数创建透明效果
Unity3D ShaderLab 使用alpha参数创建透明效果 其实Unity为了方便我们的工作,为我们内置了很多参数.比如马上用到的透明功能. 准备场景新建Shader Material ,一张 ...
- 用CSS实现Firefox 和IE 都支持的Alpha透明效果
有的时候,为了实现一些特殊效果,需要将页面元素变透明,本文介绍的就是用 CSS 实现 Firefox 和 IE 都支持的 Alpha 透明效果.CSS: filter:alpha(opacity=50 ...
随机推荐
- 绑定的jndi获得connection时,出的错,java.io.NotSerializableException
求助:java.io.NotSerializableException 最近系统频繁出现Lookup error: java.io.WriteAbortedException: Writing abo ...
- 【深入理解Java集合框架】红黑树讲解(上)
来源:史上最清晰的红黑树讲解(上) - CarpenterLee 作者:CarpenterLee(转载已获得作者许可,如需转载请与原作者联系) 文中所有图片点击之后均可查看大图! 史上最清晰的红黑树讲 ...
- django+celery +rabbitmq
celery是一个python的分布式任务队列框架,支持 分布的 机器/进程/线程的任务调度.采用典型的生产者-消费者模型 包含三部分:1. 队列 broker :可使用redis ,rabbitmq ...
- linux 查看进程下进程的数量
1.pstree -p 14686(PID) 获取到nginx的四个子进程(或 ps -ef |grep nginx) 2. cat /proc/15178(PID)/status threads即 ...
- Linux_x86下NX与ASLR绕过技术
本文介绍Linux_x86下NX与ASLR绕过技术,并对GCC的Stack Canaries保护技术进行原理分析. 本文使用存在漏洞代码如下: /* filename : sof.c */ #incl ...
- Scrapy、Scrapy-redis组件
目录 Scrapy 一.安装 二.基本使用 1. 基本命令 2.项目结构以及爬虫应用简介 3. 小试牛刀 4. 选择器 5. 格式化处理 6.中间件 7. 自定制命令 8. 自定义扩展 9. 避免重复 ...
- 安装jdk1.8
这是很普通的shell的脚本,只需要注意:echo 语句内出现的变量,需要转义! #!/bin/bash function get_current_time_stamp(){ echo `date & ...
- java 程序编译和运行过程
java整个编译以及运行的过程相当繁琐,我就举一个简单的例子说明: Java程序从源文件创建到程序运行要经过两大步骤: 1.源文件由编译器编译成字节码(ByteCode): 2.字节码由java虚拟机 ...
- eclipse中,将springboot项目打成jar包
1.右击项目,选择Run As - Maven clean 2.右击项目,选择Run As - Maven install 3.成功后 会在项目的target文件夹下生成jar包 4.将打包好的jar ...
- Day 08 文件操作模式,文件复制,游标
with open:将文件的释放交给with管理 with open('文件', '模式', encoding='utf-8') as f: # 操作 pass a模式:追加写入 # t ...