<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(透明效果)的更多相关文章

  1. WPF中ListBox滚动时的缓动效果

    原文:WPF中ListBox滚动时的缓动效果 上周工作中遇到的问题: 常规的ListBox在滚动时总是一格格的移动,感觉上很生硬. 所以想要实现类似Flash中的那种缓动的效果,使ListBox滚动时 ...

  2. 如何在WPF控件上应用简单的褪色透明效果?

    原文 https://dailydotnettips.com/how-to-create-simple-faded-transparent-controls-in-wpf/ 使用OpacityMask ...

  3. WPF通过不透明蒙板切割显示子控件

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/Backspace110/article/ ...

  4. WPF ItemsControl ListBox ListView比较

    在进行列表信息展示时,WPF中提供多种列表可供选择.这篇博客将对WPF ItemsControl, ListBox, ListView进行比较. 相同点: 1. 这三个控件都是列表型控件,可以进行列表 ...

  5. WPF 有趣的动画效果

    WPF 有趣的动画效果         这一次我要呈上一个简单的文章,关于给你的WPF apps加入美丽的光线动画,可是我对动画这东西可能有点入迷了.         实际上.我对动画如此的入迷,以至 ...

  6. WPF Windows背景透明其中的文字保持不透明

    原文:WPF Windows背景透明其中的文字保持不透明 版权声明:本文为博主原创,未经允许不得转载.交流.源码资料加群:161154103 https://blog.csdn.net/mpegfou ...

  7. Android课程---Android设置透明效果的三种方法(转)

    1.使用Android系统自带的透明效果资源 <Button  android:background="@android:color/transparent"/>   ...

  8. Unity3D ShaderLab 使用alpha参数创建透明效果

    Unity3D ShaderLab 使用alpha参数创建透明效果 其实Unity为了方便我们的工作,为我们内置了很多参数.比如马上用到的透明功能. 准备场景新建Shader Material ,一张 ...

  9. 用CSS实现Firefox 和IE 都支持的Alpha透明效果

    有的时候,为了实现一些特殊效果,需要将页面元素变透明,本文介绍的就是用 CSS 实现 Firefox 和 IE 都支持的 Alpha 透明效果.CSS: filter:alpha(opacity=50 ...

随机推荐

  1. apache 与 php-fpm 几种处理方式

    目录 1.SetHandler 2.ProxyPassMatch 3.ProxyPass 1.SetHandler 在apache配置文件只部署一次 需要Apache 2.4.9以上才行 ip:por ...

  2. 二十五. Python基础(25)--模块和包

    二十五. Python基础(25)--模块和包 ● 知识框架   ● 模块的属性__name__ # my_module.py   def fun1():     print("Hello& ...

  3. effective_java第23条:请不要新代码中使用原生态类型

    从这条开始涉及泛型相关的点. 从JDK5开始Java新增了“泛型”新特性,例如:List<String>,在这之前则只有List不会限定类型. 如今的JDK版本中还是可以写原生类型,但这会 ...

  4. 手把手教你实现一个 Vue 进度条组件!

    最近在个人的项目中,想对页面之间跳转的过程进行优化,想到了很多文档或 npm 等都用到的页面跳转进度条,于是便想自己去实现一个,特此记录. 来看下 npm 搜索组件时候的效果: so 下面咱们一起动手 ...

  5. JavaScript 注意要点

    何时加引号: 只有变量不加引号.加了引号的一定不是变量,是字符 方法:     方法一律带有小括号 js 中的作用域    全局变量: 在最外层定义的变量: 在函数体内部看是没有声明var的 也是全局 ...

  6. XQuery:查询任何可作为 XML 形态呈现的数据,包括数据库

    XQuery 也被称为 XML Query,被设计用来查询 XML 数据. 学习这个 需要知道 HTML / XHTML XML / XML 命名空间 XPath XML 实例文档 我们将在下面的例子 ...

  7. 集群容器管理之swarm ---集群部署

    集群部署及节点管理 使用swarm前提: Docker版本1.12+ 集群节点之间保证TCP 2377.TCP/UDP 7946和UDP 4789端口通信 节点规划: 操作系统:centos7.4.1 ...

  8. HBuilder设置沉浸式状态栏显示效果

    1:在[manifest.json]文件中,在[plus-->distribute--> apple]下加上[ "UIReserveStatusbarOffset":f ...

  9. I think I need a boat house

    I think I need a boat house. Fred Mapper is considering purchasing some land in Louisiana to build h ...

  10. PTA寒假二

    7-1 币值转换 (20 分) 输入一个整数(位数不超过9位)代表一个人民币值(单位为元),请转换成财务要求的大写中文格式.如23108元,转换后变成"贰万叁仟壹百零捌"元.为了简 ...