首页 技术 正文
技术 2022年11月19日
0 收藏 932 点赞 4,926 浏览 3225 个字

引言

本文就WPF中的ListBox常用项给以实例代码演示,包括隐蔽属性的设置,Style设置,以及ControlTemplate的自定义。

 

Listbox平滑滚动

<ListBox ItemsSource="{Binding ActorList}" Width="300"
ScrollViewer.CanContentScroll="False"/>
或者
 <ListBox.Template>
<ControlTemplate>
<ScrollViewer Focusable="False" CanContentScroll="True">
<ItemsPresenter />
</ScrollViewer>
</ControlTemplate>
</ListBox.Template>

 

 

参考:http://stackoverflow.com/a/3031298/1616023

StackPanel implements the IScrollInfo interface to do logical scrolling, but if ScrollViewer can’t find an IScrollInfo child it falls back to doing physical scrolling.

There are three ways to obtain logical scrolling inside a ScrollViewer:

  1. Let the ScrollViewer’s direct child be a panel that can do logical scrolling (such as a StackPanel)
  2. Let the ScrollViewer’s direct child be an ItemsPresenter which presents such a panel
  3. Let the ScrollViewer’s direct child be a custom class you write yourself that implements IScrollInfo

 

点击ListboxItem任意位置选中ListBoxItem

<ListBox.Resources>
<Style TargetType="{x:Type ListBoxItem}">
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="IsSelected" Value="True" />
</Trigger>
</Style.Triggers>
</Style>
</ListBox.Resources>

引用:Trigger SelectedIndex changed whilst clicking on any control within a ListBoxItem area

 

 

控制滚动条的显示

ScrollViewer.VerticalScrollBarVisibility="Visible"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"

 

更改Items的container

 

 <ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>

 

或者

<ListBox.Style>
<Style TargetType="{x:Type ListBox}">
<Setter Property="Visibility" Value="Visible" />
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.Style>

 

自定义ItemsControl template

<ItemsControl x:Name="activitiesControl" Margin="10">
<ItemsControl.Template>
<ControlTemplate>
<WrapPanel Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"
FlowDirection="LeftToRight" IsItemsHost="true">
</WrapPanel>
</ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Style="{DynamicResource ActionButton}" HorizontalAlignment="Right" Margin="5"
Content="{Binding Value}" Width="200"
Command="{Binding Path=ViewModel.ActionTypeCommand,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType=local:CustomerEditView}}" CommandParameter="{Binding Key}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

参考另一文章数据邦定之DataTemplate 根据对象属性切换模板

 

自定ListBox

 

A WPF Problem Solved Two Very Different Ways – Using XAML Only – Using a Custom Control

[WPF系列]-ListBox[WPF系列]-ListBox

 

Listbox 为空时显示

 

利用AdornerLayer实现如下:

[WPF系列]-ListBox

EmptyItemsControlOverlay

 

另一简单办法:

<Style TargetType="{x:Type ListBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Items.Count,
RelativeSource={RelativeSource Self}}" Value="0">
<Setter Property="Background">
<Setter.Value>
<VisualBrush Stretch="None">
<VisualBrush.Visual>
<TextBlock Text="No Items" VerticalAlignment="Center" HorizontalAlignment="Center" IsEnabled="False" />
</VisualBrush.Visual>
</VisualBrush>
</Setter.Value>
</Setter>
<!--方法二-->
<!--<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border BorderThickness="1" BorderBrush="Black"
Padding="10" Margin="10" Background="Transparent">
--><!--<TextBlock>No items to display</TextBlock>--><!--
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>-->
</DataTrigger>
</Style.Triggers>
</Style>

 

参考

WPF : Using Adorner for overlaying Empty Collections
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,557
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898