您将要在 XAML 中创建一个数据触发器,以检查每个项目是否是您要查找的项目。
这个 SO question足够相关,可以让您走上正确的轨道。它最终会看起来像这样:
<Style TargetType="TabItem">
<Style.Triggers>
<!-- Styling for elements that are NOT the last item -->
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource LastItemConverter}}" Value="False">
<Setter Property="Template">
<Setter.Value>
.
.
.
</Setter.Value>
</Setter>
</DataTrigger>
<!-- Styling for elements that are the last item -->
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource LastItemConverter}}" Value="True">
<Setter Property="Template">
<Setter.Value>
.
.
.
</Setter.Value>
</Setter>
</DataTrigger>
转换器看起来像这样 - 取决于您绑定的 UI 元素。
public class IsLastItemInContainerConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
DependencyObject item = (DependencyObject)value;
TabControl tc = (TabControl)TabControl.ItemsControlFromItemContainer(item);
// Return true for the first and last element
// (so they are similarly styled compared to other elements).
if (tc.ItemContainerGenerator.IndexFromContainer(item) == tc.Items.Count - 1) {
return true;
}
else if (tc.ItemContainerGenerator.IndexFromContainer(item) == 0)
{
return true;
}
else
{
return false;
}
}
希望有帮助。