1

下面是 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')

TabItem 样式

<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>
4

1 回答 1

0

查看 Setter Value="{Binding HoverForeground , RelativeSource={RelativeSource TemplatedParent}}" - 它获取对此元素的模板父级的引用并尝试查找属性 HoverForeground。您的 controltemplate 的模板化父级是 Tab 控件(第一种样式)...

于 2012-06-08T20:35:52.877 回答