XAML中样式控件出错"表达式类型不是有效的样式值"

时间:2017-03-31 09:34:41

标签: c# wpf xaml

我收到的错误表明我在我的XAML代码中使用了无效的表达式类型,在下面的代码中标记为<!-- Error start -->

    <ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:RDictionary="clr-namespace:DLLCustomExtension;assembly=DLLCustomExtension">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary RDictionary:MDExtension.Name="ColorBrushes" Source="Solidbrushes.xaml" />
    </ResourceDictionary.MergedDictionaries>

    <Style x:Key="PrimaryNavToggleButtons" TargetType="{x:Type ToggleButton}">
        <Setter Property="Background" Value="{StaticResource PrimaryNavButtonsBackcolor}" />
        <Setter Property="BorderBrush" Value="{x:Null}" />
        <Setter Property="Foreground" Value="{StaticResource PrimaryNavButtonsForeground}" />
        <Setter Property="HorizontalContentAlignment" Value="Center" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="VerticalAlignment" Value="Stretch" />
        <Setter Property="HorizontalAlignment" Value="Stretch" />
        <Setter Property="Margin" Value="3" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToggleButton}"> <!-- Error start -->
                    <Border
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        CornerRadius="5">
                        <Grid>
                            <ContentPresenter
                                x:Name="ContentPresenter"
                                x:Uid="ContentPresenter"
                                Visibility="Visible" />
                            <AdornerDecorator />
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsMouseOver" Value="True" />
                                <Condition Property="IsChecked" Value="False" />
                            </MultiTrigger.Conditions>
                            <MultiTrigger.Setters>
                                <Setter Property="Background" Value="{StaticResource PrimaryNavButtonHovBackcolor}" />
                            </MultiTrigger.Setters>
                        </MultiTrigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsMouseOver" Value="False" />
                                <Condition Property="IsChecked" Value="False" />
                            </MultiTrigger.Conditions>
                            <MultiTrigger.Setters>
                                <Setter Property="Background" Value="{TemplateBinding Background}" />
                            </MultiTrigger.Setters>
                        </MultiTrigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsMouseOver" Value="True" />
                                <Condition Property="IsChecked" Value="True" />
                            </MultiTrigger.Conditions>
                            <MultiTrigger.Setters>
                                <Setter Property="Background" Value="{StaticResource PrimaryNavButtonHovBackcolor}" />
                            </MultiTrigger.Setters>
                        </MultiTrigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsMouseOver" Value="False" />
                                <Condition Property="IsChecked" Value="True" />
                            </MultiTrigger.Conditions>
                            <MultiTrigger.Setters>
                                <Setter Property="Background" Value="{StaticResource PrimaryNavButtonHovBackcolor}" />
                            </MultiTrigger.Setters>
                        </MultiTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate> <!-- Error end -->
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

那我做错了什么?感谢。

1 个答案:

答案 0 :(得分:4)

<Setter Property="Background" Value="{TemplateBinding Background}" />

此行生成错误。 TemplateBinding在ContentTemplate Trigger设置器中不起作用。它应该使用BindingRelativeSource TemplatedParent一起编写。

<Setter Property="Background" 
        Value="{Binding Background, RelativeSource={RelativeSource TemplatedParent}}" />


但是我不明白这条线的目的。模板部分中已有{TemplateBinding Background}。为什么要在触发器中重复它?