Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An object of the type "System.Windows.Controls.ControlTemplate" cannot be applied to a property that expects the type "System.Windows.Style"

In my following code

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

I am trying to use the style which is written in Generic.Xaml and it is as followes

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

But I am getting the following error -An object of the type "System.Windows.Controls.ControlTemplate" cannot be applied to a property that expects the type "System.Windows.Style"

I need help in this.regards

like image 475
Payel Mukherjee Chatterjee Avatar asked Mar 19 '23 21:03

Payel Mukherjee Chatterjee


2 Answers

You need to use Template={StaticResource SearchUCHeaderButtonsStyle}instead of `Style="{StaticResource SearchUCHeaderButtonsStyle}" as you have edited Templet property of button not style.

like image 85
Heena Avatar answered Apr 06 '23 05:04

Heena


you are trying to set the style of ControlTemplate in a control which is button . So this error is coming. To avoid the above error you have to do

<Button Height="25" Width="50">
   <Button.Style>
     <Style TargetType="Button">
       <Setter Property="Template"
               Value="{StaticResource SearchUCHeaderButtonsStyle}"/>
     </Style>
   </Button.Style>
</Button>
like image 24
Anindya Avatar answered Apr 06 '23 03:04

Anindya