在我下面的代码中
<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>
但我收到以下错误 - “System.Windows.Controls.ControlTemplate”类型的对象不能应用于需要“System.Windows.Style”类型的属性
我需要帮助。问候
最佳答案
您需要使用 Template={StaticResource SearchUCHeaderButtonsStyle}
而不是 `Style="{StaticResource SearchUCHeaderButtonsStyle}"因为您编辑的是按钮的 Templet 属性而不是样式。
关于c# - 类型为 "System.Windows.Controls.ControlTemplate"的对象不能应用于需要类型为 "System.Windows.Style"的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23064447/