WPF ListBox 一些小知识点
页面代码:
<Grid Grid.Row="0" Grid.Column="2"> <ListBox x:Name="lvStep" Style="{StaticResource StepTemp}" Width="830" HorizontalAlignment="Left" ItemsSource="{Binding DataSteps,Mode=OneWay}" MouseLeftButtonUp="lvStep_MouseLeftButtonUp" TouchUp="lvStep_TouchUp"> </ListBox>
资源样式代码:
1 <Style TargetType="ListBox" x:Key="StepTemp"> 2 <Style.Resources> 3 <SolidColorBrush Color="Transparent" x:Key="{x:Static SystemColors.HighlightBrushKey}"/> 4 </Style.Resources> 5 <Setter Property="Foreground" Value="#FFDEDEDD"></Setter> 6 <Setter Property="BorderBrush" Value="#00000000"></Setter> 7 <Setter Property="Background" Value="#00000000"></Setter> 8 <Setter Property="FontSize" 9 Value="24"/> 10 <Setter Property="FontFamily" Value="Microsoft YaHei UI"></Setter> 11 <Setter Property="ItemTemplate"> 12 <Setter.Value> 13 <DataTemplate> 14 <StackPanel Orientation="Horizontal" Height="45" > 15 <TextBlock Text="{Binding SerialNum}" Foreground="{Binding TextBrush}" VerticalAlignment="Center"></TextBlock> 16 <TextBlock Foreground="{Binding TextBrush}">.</TextBlock> 17 <TextBlock Text="{Binding StepDescription}" Foreground="{Binding TextBrush}" FontSize="18" VerticalAlignment="Center"> 18 </TextBlock> 19 20 </StackPanel> 21 </DataTemplate> 22 </Setter.Value> 23 </Setter> 24 </Style>
1.Style="{StaticResource StepTemp}"
2.TargetType="ListBox" x:Key="StepTemp"
可以在资源样式代码中设置数据模板,没必要把数据模板写到页面代码中。
<SolidColorBrush Color="Transparent" x:Key="{x:Static SystemColors.HighlightBrushKey}"/> 设置点击行背景为透明色,当然也可以设置其他背景色,和字体颜色。
Foreground="{Binding TextBrush}" 数据模板中的控件 绑定这个TextBrush属性。就可以在代码中设置,然后驱动页面字体颜色改变。
这种带数据模板的,<StackPanel Orientation="Horizontal" Height="45" >可以设置数据的每一行的内容为横向显示。
想要所有行都横向显示:
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True"></WrapPanel>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
ItemsPanel=》ItemsPanelTemplate=》WrapPanel IsItemsHost="True"
先写这么多吧,遇到问题就写点。