WPF中ListBox的使用注意事项
1.style
注意绿色标注处,如果想要在Listbox外使用滚动条,此处则要注销ScrollViewer,否则在界面区域只能显示ListBox时会出现点击一下,弹到顶端的问题。
<Grid.Resources>
<!--SelectedItem with focus -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FAE388" Opacity="1"/>
<!--SelectedItem without focus-->
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="LightBlue" Opacity="0.4"/>
<Style TargetType="ListBoxItem">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<!--<Setter Property="Opacity" Value="0.8"/>-->
<Setter Property="Background" Value="#F0F0F0"/>
<Setter Property="Foreground" Value="Black"/>
</Trigger>
<Trigger Property="IsFocused" Value="true">
<Setter Property="Background" Value="Coral"/>
<Setter Property="Foreground" Value="Black"/>
</Trigger>
</Style.Triggers>
</Style>
<Style TargetType="{x:Type ListBox}">
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate DataType="{x:Type baseModel:FlowTempleteModel}">
<Grid Margin="10,0,5,0">
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
<!-- 定义子元素的布局容器, 比如:横向,纵向-->
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<!-- 定义ListBox自身外观, 比如: 圆角边框-->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBox">
<Border CornerRadius="5" Background="{TemplateBinding ListBox.Background}">
<!--<ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">-->
<ItemsPresenter>
</ItemsPresenter>
<!--</ScrollViewer>-->
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>

浙公网安备 33010602011771号