Стиль кнопки по умолчанию не применяется, если добавлены триггеры - PullRequest
0 голосов
/ 19 июня 2020

Я определил стиль по умолчанию для всех кнопок моего приложения в App.xaml:

<Style TargetType="{x:Type Button}">
    <Setter Property="FontSize" Value="14"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border 
                    x:Name="Border"  
                    CornerRadius="2" 
                    BorderThickness="1"
                    Background="{StaticResource WindowBorderColor}"
                    BorderBrush="{StaticResource WindowBorderColor}">
                    <ContentPresenter Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Background" Value="{StaticResource InactiveBackgroundColor}"/>
                        <Setter Property="Foreground" Value="{StaticResource InactiveForegroundColor}"/>
                        <Setter Property="Opacity" Value="0.5"/>
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="{StaticResource HeaderButtonOverColor}"/>
                        <Setter Property="Foreground" Value="{StaticResource HeaderForeHighlightColor}"/>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter Property="Background" Value="{StaticResource HeaderButtonPressedColor}"/>
                        <Setter Property="Foreground" Value="{StaticResource WindowForeColor}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Но когда я хочу добавить триггер к моей кнопке, стиль по умолчанию больше не учитывается:

<Button Content="{x:Static p:Resources.Delete}" Click="DeleteMacro_Click" Margin="3" >
    <Button.Style>
        <Style TargetType="{x:Type Button}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding MacroLeft.Name, Mode=OneWay}" Value="">
                    <Setter Property="Button.IsEnabled" Value="False" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

Я обнаружил, что ссылка , поэтому я добавил TargetType="{x:Type Button}" в свой WPF, но ничего не изменилось. Почему не учитывается стиль по умолчанию, и как я могу решить эту проблему, не создавая другой c стиль в моем app.xaml?

1 Ответ

1 голос
/ 19 июня 2020

Вы можете сделать стили, определенные в app.xaml, в качестве базовых следующим образом:

<Button.Style>
     <Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">

     </Style>
</Button.Style>
...