类型为" System.Windows.Controls.ControlTemplate"的对象;无法应用于需要类型" System.Windows.Style"的属性。

时间:2014-04-14 15:45:54

标签: c# wpf silverlight xaml prism

在我的以下代码中

<StackPanel Orientation="Horizontal" Grid.Row="0" Grid.Column="0">
            <TextBlock Text="Opportunity" Height="25" Width="50"></TextBlock>
            <Button Height="25" Width="50" Style="{StaticResource SearchUCHeaderButtonsStyle}">

            </Button>
            <Button Height="25" Width="50"></Button>
        </StackPanel>

我正在尝试使用Generic.Xaml中编写的样式,并且如下所示

<ControlTemplate x:Key="SearchUCHeaderButtonsStyle" TargetType="Button">
        <Border Name="Border" CornerRadius="2" BorderThickness="1" Background="#C0C0C0" BorderBrush="#404040">
            <ContentPresenter Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True"/>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsKeyboardFocused" Value="true">
                <Setter TargetName="Border" 
                          Property="BorderBrush" Value="#202020" />
            </Trigger>
            <Trigger Property="IsDefaulted" Value="true">
                <Setter TargetName="Border" 
                          Property="BorderBrush" Value="#202020" />
            </Trigger>
            <Trigger Property="IsMouseOver" Value="true">
                <Setter TargetName="Border" 
                          Property="Background" Value="#808080" />
            </Trigger>
            <Trigger Property="IsPressed" Value="true">
                <Setter TargetName="Border" 
                          Property="Background" Value="#E0E0E0" />
                <Setter TargetName="Border" 
                          Property="BorderBrush" Value="#606060" />
            </Trigger>
            <Trigger Property="IsEnabled" Value="false">
                <Setter TargetName="Border" 
                          Property="Background" Value="#EEEEEE" />
                <Setter TargetName="Border" 
                          Property="BorderBrush" Value="#AAAAAA" />
                <Setter Property="Foreground" Value="#888888"/>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

但是我收到以下错误 - 类型&#34; System.Windows.Controls.ControlTemplate&#34;的对象不能应用于期望类型的属性&#34; System.Windows.Style&#34;

我在this.regards中需要帮助

2 个答案:

答案 0 :(得分:3)

你需要使用Template={StaticResource SearchUCHeaderButtonsStyle}而不是'Style =&#34; {StaticResource SearchUCHeaderButtonsStyle}&#34;因为你已经编辑了按钮而不是样式的Templet属性。

答案 1 :(得分:3)

您正尝试在控件(即按钮)中设置ControlTemplate的样式。所以这个错误即将来临。要避免上述错误,您必须执行

<Button Height="25" Width="50">
   <Button.Style>
     <Style TargetType="Button">
       <Setter Property="Template"
               Value="{StaticResource SearchUCHeaderButtonsStyle}"/>
     </Style>
   </Button.Style>
</Button>