打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
WPF中Listbox使用

在使用LsitBox这个控件的时候,如果添加数据绑定,只需要将要显示的结构体绑定到 ItemsSource 就可以将结构体成员显示出来。但如果结构体内有多项,而我们只想显示其中一项的话,可以这样

 <Style TargetType="ListBoxItem">

<Setter Property="Foreground" Value="Black"/>

<Setter Property="OverridesDefaultStyle" Value="True"/>

<Setter Property="Height" Value="30"/>

<Setter Property="FontSize" Value="16"/>

<Setter Property="Template">

<Setter.Value>

<ControlTemplate TargetType="ListBoxItem">

<Border Name="back" BorderBrush="{x:Null}" Background="{x:Null}" BorderThickness="0,1,0,1">

<ContentPresenter Margin="2" VerticalAlignment="Center" Content="{Binding 结构体内成员名字}" />

</Border>

</ControlTemplate>

</Setter.Value>

</Setter>

</Style>

如果想给listbox添加类似鼠标移动到每一项的时候有一定的效果,可以在它的资源字典里直接添加字典,而在页面则不用给listbox控件作任何 类似stye={};的东西,这一点比较特别,它好像全局变量一样,只要使用该资源字典,则Listbox 就会显示字典定义的效果。

贴一个常用的Listbox的资源字典

 1  <Style TargetType="ListBox"> 2  3         <Setter Property="BorderBrush" Value="Transparent"/> 4  5         <Setter Property="BorderThickness" Value="1"/> 6  7         <Setter Property="Background" Value="Transparent"/> 8  9         <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Hidden"/>10 11         <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>12 13         <Setter Property="ScrollViewer.CanContentScroll" Value="true"/>14 15         <Setter Property="VerticalContentAlignment" Value="Center"/>16 17 18     </Style>19 20     <Style TargetType="ListBoxItem">21         <Setter Property="Foreground" Value="#AEAEAE" />22         <Setter Property="FontSize" Value="13" />23         <Style.Triggers>24             <Trigger Property="IsMouseOver" Value="True">25                 <Setter Property="Background">26                     <Setter.Value>27                         <LinearGradientBrush StartPoint="1,0" EndPoint="0,1">28                             <GradientStop Color="#FF013B48" Offset="0" />29                             <GradientStop Color="#FF054C58" Offset="1" />30                             <GradientStop Color="#FF2B809A" Offset="0.295" />31                             <GradientStop Color="#FF287D96" Offset="0.68" />32                         </LinearGradientBrush>33                     </Setter.Value>34                 </Setter>35                 <Setter Property="Foreground" Value="White" />36             </Trigger>37 38         </Style.Triggers>39         <Style.Resources>40             41             <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#00B4DF" Opacity=".4"/>42             43             <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#00B4DF" Opacity=".4"/>44         </Style.Resources>45 46     </Style>

再贴一个与上面若有重复的样式,但此样式包括在样式里绑定结构体成员的完整样式

 <!--ListBox样式-->

<Style TargetType="ListBox">

<Setter Property="BorderBrush" Value="{x:Null}"/>

<Setter Property="BorderThickness" Value="0"/>

<Setter Property="Background" Value="{x:Null}"/>

<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>

<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>

<Setter Property="ScrollViewer.CanContentScroll" Value="true"/>

<Setter Property="VerticalContentAlignment" Value="Center"/>

</Style>

<!--ListBoxItem样式-->

<Style TargetType="ListBoxItem">

<Setter Property="Foreground" Value="Black"/>

<Setter Property="OverridesDefaultStyle" Value="True"/>

<Setter Property="Height" Value="30"/>

<Setter Property="FontSize" Value="16"/>

<Setter Property="Template">

<Setter.Value>

<ControlTemplate TargetType="ListBoxItem">

<Border Name="back" BorderBrush="{x:Null}" Background="{x:Null}" BorderThickness="0,1,0,1">

<ContentPresenter Margin="2" VerticalAlignment="Center" Content="{Binding SourcePacketName}" />

</Border>

<ControlTemplate.Triggers>

<Trigger Property="IsSelected" Value="True">

<Trigger.EnterActions>

<BeginStoryboard>

<Storyboard>

<ColorAnimation To="Red" Duration="0:0:0.2" Storyboard.TargetProperty="(ListBoxItem.Foreground).(SolidColorBrush.Color)" />

</Storyboard>

</BeginStoryboard>

</Trigger.EnterActions>

<Trigger.ExitActions>

<BeginStoryboard>

<Storyboard>

<ColorAnimation Duration="0:0:0.2" Storyboard.TargetProperty="(ListBoxItem.Foreground).(SolidColorBrush.Color)" />

</Storyboard>

</BeginStoryboard>

</Trigger.ExitActions>

</Trigger>

<Trigger Property="IsMouseOver" Value="True">

<Trigger.EnterActions>

<BeginStoryboard>

<Storyboard>

<ColorAnimation To="Blue" Duration="0:0:0.2" Storyboard.TargetProperty="(ListBoxItem.Foreground).(SolidColorBrush.Color)" />

</Storyboard>

</BeginStoryboard>

</Trigger.EnterActions>

<Trigger.ExitActions>

<BeginStoryboard>

<Storyboard>

<ColorAnimation Duration="0:0:0.2" Storyboard.TargetProperty="(ListBoxItem.Foreground).(SolidColorBrush.Color)" />

</Storyboard>

</BeginStoryboard>

</Trigger.ExitActions>

</Trigger>

</ControlTemplate.Triggers>

</ControlTemplate>

</Setter.Value>

</Setter>

</Style>
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
WPF自定义控件与样式(6)
【WPF学习】第六十二章 构建更复杂的模板
WPF listbox 选中效果
Trigger
潜移默化学会WPF(绚丽篇)--RadioButton
CSDN 论坛
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服