下面是TabItem样式的一部分,我的绑定错误类似于下面显示的那样。
如何清理代码?
System.Windows.Data Error: 40 : BindingExpression path error: 'HoverForeground' property not found on 'object' ''TabItem' (Name='')'. BindingExpression:Path=HoverForeground; DataItem='TabItem' (Name=''); target element is 'GlyphButton' (Name='HideButton'); target property is 'Foreground' (type 'Brush')
<Style x:Key="OrangeTabItemStyle" TargetType="{x:Type TabItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Border AllowDrop="true" ToolTip="{Binding DisplayName}">
<Border Name="Border" Background="Transparent" BorderBrush="Transparent" BorderThickness="1,1,1,0" CornerRadius="2,2,0,0">
<DockPanel x:Name="TitlePanel" TextElement.Foreground="{StaticResource FileTabTextBrush}">
****** <ctrl:GlyphButton x:Name="HideButton"
DockPanel.Dock="Right"
GlyphForeground="Transparent"
HoverBackground="{StaticResource ToolWindowButtonHoverActiveBackgroundBrush}"
HoverBorderBrush="{StaticResource ToolWindowButtonHoverActiveBorderBrush}"
***** HoverForeground="{StaticResource ToolWindowButtonHoverActiveGlyphForegroundBrush}"
PressedBackground="{StaticResource ToolWindowButtonPressedBackgroundBrush}"
PressedBorderBrush="{StaticResource ToolWindowButtonPressedBorderBrush}"
PressedForeground="{StaticResource ToolWindowButtonPressedActiveGlyphForegroundBrush}"
HoverBorderThickness="1" PressedBorderThickness="1" Margin="3,2,3,4"
Command="{Binding CloseCommand}"
CommandParameter="{Binding}"
ToolTip="Close">
<Path x:Name="CloseButtonStroke" Width="10" Height="8" Stretch="Uniform"
Data="F1 M 0,0 L 2,0 5,3 8,0 10,0 6,4 10,8 8,8 5,5 2,8 0,8 4,4 0,0 Z"
Fill="{Binding Path=(TextElement.Foreground), RelativeSource={RelativeSource Self}}" />
</ctrl:GlyphButton>
<ContentPresenter x:Name="Content" HorizontalAlignment="Stretch" Margin="4,2,4,4" VerticalAlignment="Stretch"
RecognizesAccessKey="true" ContentSource="Header" />
</DockPanel>
</Border>
<Border.InputBindings>
<KeyBinding Command="{Binding CloseCommand}" Key="F4" Modifiers="Control" />
</Border.InputBindings>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="Border" Value="{StaticResource FileTabIsMouseOverBorderBackgroundBrush}" Property="Background" />
<Setter TargetName="Border" Value="{StaticResource FileTabIsMouseOverBorderBrush}" Property="BorderBrush" />
<Setter TargetName="TitlePanel" Value="{StaticResource FileTabIsMouseOverTextForegroundBrush}" Property="TextElement.Foreground" />
<Setter TargetName="HideButton" Value="{StaticResource FileTabIsMouseOverGlyphForegroundBrush}" Property="GlyphForeground" />
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Panel.ZIndex" Value="1" />
<Setter TargetName="Border" Value="{StaticResource FileTabIsSelectedBackgroundBrush}" Property="Background" />
<Setter TargetName="Border" Value="{StaticResource FileTabIsSelectedBackgroundBrush}" Property="BorderBrush" />
<Setter TargetName="Border" Property="BorderThickness" Value="0" />
<Setter TargetName="Border" Property="Padding" Value="0,1,0,0" />
<Setter TargetName="HideButton" Property="Margin" Value="3" />
<Setter TargetName="TitlePanel" Value="{StaticResource FileTabIsSelectedTextForegroundBrush}" Property="TextElement.Foreground" />
<Setter TargetName="HideButton" Value="{StaticResource FileTabIsSelectedGlyphForegroundBrush}" Property="GlyphForeground" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
public class GlyphButton : Button
{
public static readonly DependencyProperty HoverForegroundProperty = DependencyProperty
.Register("HoverForeground", typeof(Brush), typeof(GlyphButton));
public Brush HoverForeground
{
get { return (Brush) GetValue(HoverForegroundProperty); }
set { SetValue(HoverForegroundProperty, value); }
}
// other properties similar to this omitted....
}
<Style TargetType="{x:Type ctrl:GlyphButton}">
<Setter Property="Width" Value="{Binding Path=ActualHeight, RelativeSource={RelativeSource Self}}" />
<Setter Property="Foreground" Value="{Binding Path=GlyphForeground, RelativeSource={RelativeSource Self}}" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="Focusable" Value="false" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ctrl:GlyphButton}">
<Border
Name="Border" Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="Border" Value="{Binding HoverBackground , RelativeSource={RelativeSource TemplatedParent}}"
Property="Background" />
<Setter TargetName="Border" Value="{Binding HoverBorderBrush , RelativeSource={RelativeSource TemplatedParent}}"
Property="BorderBrush" />
<Setter TargetName="Border" Value="{Binding HoverBorderThickness , RelativeSource={RelativeSource TemplatedParent}}"
Property="BorderThickness" />
<Setter Value="{Binding HoverForeground , RelativeSource={RelativeSource TemplatedParent}}"
Property="Foreground" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="Border" Value="{Binding PressedBackground , RelativeSource={RelativeSource TemplatedParent}}"
Property="Background" />
<Setter TargetName="Border" Value="{Binding PressedBorderBrush , RelativeSource={RelativeSource TemplatedParent}}"
Property="BorderBrush" />
<Setter TargetName="Border" Value="{Binding PressedBorderThickness , RelativeSource={RelativeSource TemplatedParent}}"
Property="BorderThickness" />
<Setter Value="{Binding PressedForeground , RelativeSource={RelativeSource TemplatedParent}}"
Property="Foreground" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
答案 0 :(得分:0)
查看Setter Value =“{Binding HoverForeground,RelativeSource = {RelativeSource TemplatedParent}}” - 它获取对此元素的模板父项的引用,并尝试查找属性HoverForeground。 controltemplate的模板化父级是Tab控件(第一种格式)......