内容简介:翻译自:https://stackoverflow.com/questions/12125764/change-style-of-last-item-in-listbox
我有列表框控件,其中包含颜色列表.这是代码和图片:
<ListBox Name="FillSelections" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Center" SelectedItem="{Binding SelectedColor}" SelectionMode="Single" Style="{StaticResource HorizontalListBoxStyle}" ItemsSource="{Binding FillColors}" ItemTemplate="{StaticResource ColorsItemTemplate}"></ListBox>
<DataTemplate x:Key="ColorsItemTemplate">
<Border BorderBrush="Transparent">
<Rectangle Width="20" StrokeThickness="1" Stroke="Black">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding}" />
</Rectangle.Fill>
</Rectangle>
</Border>
图片:
我如何才能像这样更改最后一项的样式:
这可以通过转换器来实现,转换器的工作是查找列表框中的最后一项 –
变流器
public class IsLastItemInContainerConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
DependencyObject item = (DependencyObject)value;
ItemsControl ic = ItemsControl.ItemsControlFromItemContainer(item);
return ic.ItemContainerGenerator.IndexFromContainer(item)
== ic.Items.Count - 1;
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
使用它你可以在你的xaml类中设置DataTemplate,如下所示 –
<ListBox ItemContainerStyle="{StaticResource ColorsItemStyle}"/>
<Style x:Key="ColorsItemStyle">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self},
Converter= StaticResource IsLastItemInContainerConverter}}" Value="False">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate></DataTemplate> // Your template goes here
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self},
Converter= StaticResource IsLastItemInContainerConverter}}" Value="True">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate></DataTemplate> // Your lastItem template goes here
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
翻译自:https://stackoverflow.com/questions/12125764/change-style-of-last-item-in-listbox
以上所述就是小编给大家介绍的《wpf – 更改ListBox中最后一项的样式》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 1.6 常用CSS样式2:其它样式
- 1.5 常用CSS样式1:文本样式
- MongoDB更改oplog大小
- ios – 更改NSURL的方案
- Vagrant更改默认的SSH端口
- Firefox 75.0 发布,地址栏更改
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Smarter Than You Think
Clive Thompson / Penguin Press HC, The / 2013-9-12 / USD 27.95
It's undeniable—technology is changing the way we think. But is it for the better? Amid a chorus of doomsayers, Clive Thompson delivers a resounding "yes." The Internet age has produced a radical new ......一起来看看 《Smarter Than You Think》 这本书的介绍吧!