我的每个var database = require('../Code/Database/Database');
var expect = require('chai').expect;
//Tests for the Database class.
describe("#Database", function(){
//Test for the makeConnection method
describe("#makeConnection", function(){
it("should make a connection with the database", function(){
expect(database.isConnected).to.be.false;
var connection = database.makeConnection();
expect(database.isConnected).to.be.true;
})
})
})
都有一个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
改为相同但结果相同)。
有什么想法解决这个问题吗?
答案 0 :(得分:1)
如果您想使用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++;
}
}
}