[英]Change tabitem style when selected item changes
我有一個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
(如果integer始終返回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
。 但是我的Property在TabControl
加載時僅設置了一次。 Trigger
總是返回相同的值,為什么屬性設置一次?
當我能夠設置此屬性時,我的目標是添加一個DataTrigger
如下所示:
<DataTrigger Property="IsItemAfterSelected" Value="True">
<Setter Property="Background" Value="Red" />
</DataTrigger>
我不知道我的方法是否最簡單,但是我沒有找到更好的解決方案(我嘗試刪除StyleValueTrue
並放入True
但結果相同)。
有什么想法可以解決這個問題?
如果要使用Binding
來執行此操作:
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++;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.