WPF中Listbox使用
来源:互联网 发布:mac桌面便签软件 编辑:程序博客网 时间:2024/05/22 22:15
在使用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>
- WPF中Listbox使用
- WPF中ListBox控件的使用
- WPF中ListBox与PropertyChanged
- WPF中一个ListBox实例
- WPF ListBox
- WPF ListBox
- WPF ListBox
- WPF中ListBox 横排的方法+ScrollView
- WPF listbox中Checkbox横向排列
- WPF 修改listbox中scollviewer的样式
- WPF中ListBox的WrapPanel布局
- WPF中ListBox滚动条自动滚动
- WPF—— ListBox的使用
- 【WPF】ListBox使用UserContrl作为子控件
- VC中ListBox使用
- listbox中使用svg图片
- WPF中,ListBox与数据间的绑定
- WPF中确保显示ListBox选中的Item
- android内核字符驱动设备实战之----------应用层测试程序篇
- GNU C与ANSI C (1)
- Matches UVA 11375
- u盘文件系统问题
- 超级牛人在华为工作十年的感悟
- WPF中Listbox使用
- 让javascript显原型!
- svn 仓库的创建和删除
- 正则表达式将字符串中的所有连续空格替换成一个空格
- c#开发ActiveX完全攻略
- c++ 引用的理解
- 解决UIScrollView截获touch事件的一个极其简单有效的办法
- Oracle删除表的几种方法
- ubuntu64 chrome 安装flash插件