当所选项更改时,更改表项样式

本文关键字:样式 选项 | 更新日期: 2023-09-27 17:52:42

我有一个TabControl和我的每个TabItem(与StyleSelector)的样式。我希望每个TabItem在所选项目更改时更改其样式。

所以我创建了一个TabItemCustom属性IsItemAfterSelected:
public class TabItemCustom: TabItem
{
    public static readonly DependencyProperty IsItemAfterSelectedProperty =
    DependencyProperty.Register("IsItemAfterSelected",
        typeof(bool),
        typeof(TabItemCustom),
        new PropertyMetadata(false));
    public bool IsItemAfterSelected
    {
        get { return (bool)GetValue(IsItemAfterSelectedProperty ); }
        set { SetValue(IsItemAfterSelectedProperty , value); }
    }
}

如果选择了下一项,则此属性必须包含True

我可以知道SelectedIndex何时变化与此DataTrigger:

<sys:Boolean x:Key="StyleValueTrue">True</sys:Boolean>
<DataTrigger Binding="{Binding Path=SelectedIndex, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabControl}}, Mode=TwoWay, Converter={StaticResource ConverterTabItemAriane}}" Value="{StaticResource StyleValueTrue}">
    <Setter Property="IsItemAfterSelected" Value="{Binding Converter={StaticResource ConverterTabItemAriane}, RelativeSource={RelativeSource Self}}"/>
 </DataTrigger>

我使用相同的转换器来检测是否为SelectedIndex(如果整数总是返回True)并设置我的值:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    if (value is DependencyObject)
    {
        DependencyObject container = (DependencyObject)value;
        var itemsControl = ItemsControl.ItemsControlFromItemContainer(container);
        var itemIndex = itemsControl.ItemContainerGenerator.IndexFromContainer(container);
        if (itemsControl is TabControl)
        {
            TabControl tabcontrol = (TabControl)itemsControl;
            if (tabcontrol.SelectedIndex >= itemIndex) return true;
            else return false;
        }
    }
    else if (value is int) return true;// selected index has changed
        return false;
    }

我已经检查了断点:当我更改所选项目时,它会进入转换器并返回True。但是我的属性只在TabControl加载时设置一次。Trigger总是返回相同的值,为什么属性设置一次?

当我能够设置这个属性时,我的目标是添加一个DataTrigger,像这样:

<DataTrigger Property="IsItemAfterSelected" Value="True">
    <Setter Property="Background" Value="Red" />
</DataTrigger>

我不知道我的方法是最简单的,但我没有找到一个更好的解决方案(我试过删除StyleValueTrue,把True代替,但结果相同)。

有解决这个问题的想法吗?

当所选项更改时,更改表项样式

如果您想使用Binding s:

XAML:

<Style TargetType="{x:Type local:TabItemCustom}">
    <Setter Property="IsItemAfterSelected">
        <Setter.Value>
            <MultiBinding Converter="{StaticResource ConverterTabItemAriane}">
                <Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type TabControl}}"/>
                <Binding RelativeSource="{RelativeSource Self}"/>
                <Binding Path="SelectedIndex" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type TabControl}}"/>
            </MultiBinding>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsItemAfterSelected" Value="True">
            <Setter Property="Background" Value="Red" />
        </Trigger>
    </Style.Triggers>
</Style>

转换器:

public class ConverterTabItemAriane : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        TabControl tabControl = values[0] as TabControl;
        TabItem tabItem = values[1] as TabItem;
        if (tabControl != null && tabItem != null && values[2] is int)
        {
            var selectedIndex = (int)values[2];
            var itemIndex = tabControl.ItemContainerGenerator.IndexFromContainer(tabItem);
            return selectedIndex >= itemIndex;
        }
        return false;
    }
    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

如果你想在你的TabControl:

XAML:

<Style TargetType="{x:Type local:TabItemCustom}">
    <Style.Triggers>
        <Trigger Property="IsItemAfterSelected" Value="True">
            <Setter Property="Background" Value="Red" />
        </Trigger>
    </Style.Triggers>
</Style>

TabControl:

public class TabControlCustom : TabControl
{
    protected override System.Windows.DependencyObject GetContainerForItemOverride()
    {
        return new TabItemCustom();
    }
    protected override void OnSelectionChanged(SelectionChangedEventArgs e)
    {
        base.OnSelectionChanged(e);
        int i = 0;
        foreach (var item in Items)
        {
            var tabItem = ItemContainerGenerator.ContainerFromItem(item) as TabItemCustom;
            if (tabItem != null)
            {
                tabItem.IsItemAfterSelected = SelectedIndex >= i;
            }
            i++;
        }
    }
}