我想以编程方式将Background
TabItem
属性绑定到自定义属性。到目前为止,自定义背景仅在未选择TabItem
时应用:
TabItem
与Bindings一起以编程方式生成:
Binding binding = new Binding();
binding.Source = this;
binding.Converter = this;
binding.Path = new PropertyPath("HasChanged");
binding.Mode = BindingMode.OneWay;
binding.TargetNullValue = tabItem.Background;
binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
BindingOperations.SetBinding(tabItem, TabItem.BackgroundProperty, binding);
我实现了接口IValueConverter
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
GradientStopCollection gradient = new GradientStopCollection();
gradient.Add(new GradientStop(Color.FromRgb(255, 234, 199), 0));
gradient.Add(new GradientStop(Color.FromRgb(255, 233, 194), 0.5));
gradient.Add(new GradientStop(Color.FromRgb(255, 225, 173), 0.5));
gradient.Add(new GradientStop(Color.FromRgb(255, 221, 163), 1));
Brush brush = new LinearGradientBrush(gradient, 90);
return (bool)value ? brush : null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
如何绑定属性以影响所选的TabItem
?
答案 0 :(得分:1)
正如问题评论中 mm8 提供的链接所述,您需要覆盖TabItem的ControlTemplate。下面是TabControl的实现,左侧是选项卡,TabItem是自定义ControlTemplate。通过更改边框的粗细来突出显示所选项目:
<TabControl TabStripPlacement="Left"
VerticalAlignment="Stretch"
HorizontalContentAlignment="Stretch"
Name="tc"
ItemsSource="{Binding MySource, Mode=OneWay}"
ContentTemplate="{StaticResource templateForTheContent}"
ItemTemplate="{StaticResource templateForTheHeader}">
<TabControl.Resources>
<Style TargetType="TabItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TabItem">
<Border Name="brdPanel"
CornerRadius="3,0,0,3"
BorderBrush="Black"
Background="{TemplateBinding Background}"
Margin="2,2,0,2"
BorderThickness="1,1,0,1">
<ContentPresenter VerticalAlignment="Center"
HorizontalAlignment="Center"
ContentSource="Header"
Margin="10,2" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected"
Value="True">
<Setter TargetName="brdPanel"
Property="BorderThickness"
Value="2,2,0,2" />
<Setter TargetName="brdPanel"
Property="Margin"
Value="1,1,0,1" />
</Trigger>
<Trigger Property="IsSelected"
Value="False">
<Setter TargetName="brdPanel"
Property="BorderThickness"
Value="1,1,0,1" />
<Setter TargetName="brdPanel"
Property="Margin"
Value="2,2,0,2" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</TabControl.Resources>
</TabControl>
在Background的绑定下面手动设置为LightCoral(此处绑定源的设置与OP&#39; s不同,仅用于演示):
MyItemVM myItem=this.tc.Items[0];
TabItem tabItem = (TabItem)this.tc.ItemContainerGenerator.ContainerFromItem(myItem);
Binding binding = new Binding();
binding.Source = myItem;
binding.Path = new PropertyPath("GroupBackground");
binding.Mode = BindingMode.OneWay;
binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
BindingOperations.SetBinding(tabItem, TabItem.BackgroundProperty, binding);
myItem.GroupBackground = Brushes.LightCoral;
MyItemVM myItem=this.tc.Items[1];
tabItem = (TabItem)this.tc.ItemContainerGenerator.ContainerFromItem(myItem);
binding = new Binding();
binding.Source = myItem;
binding.Path = new PropertyPath("GroupBackground");
binding.Mode = BindingMode.OneWay;
binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
BindingOperations.SetBinding(tabItem, TabItem.BackgroundProperty, binding);
myItem.GroupBackground = Brushes.LightCoral;
请注意,这只会更改TabItem标题的背景。
答案 1 :(得分:0)
我最终使用 Arie的答案的部分内容,但我无法使用他的模板,因为我想要默认的Windows标签样式。
我的第一步是删除所有程序生成的绑定和转换器。然后我使用Tag
的{{1}}属性作为布尔值来标记应突出显示哪些选项卡。
TabItem
正如Arie所说,我需要在我的XAML文件中覆盖tabItem.Tag = true;
的模板。我使用this method生成默认模板TabItem
。
最后一步是在触发器中定义一些Setter,以检查TabItem
属性是否设置为Tag
。此外,您需要定义true
应该转换为布尔值而不是字符串。为此,您需要在Tag
元素中定义sys
:
Window
将属性<Window ...
xmlns:sys="clr-namespace:System;assembly=mscorlib"
... >
解释为布尔值:
Tag
生成的<Trigger.Value>
<sys:Boolean>True</sys:Boolean>
</Trigger.Value>
模板:
TabItem