C# WPF自定义控件不使用ToggleButton的默认样式

C# WPF自定义控件不使用ToggleButton的默认样式,c#,wpf,custom-controls,C#,Wpf,Custom Controls,我正在创建一个基于ToggleButton的自定义控件。 使用Generic.xaml中的这个简单样式,我无法在自定义控件上使用默认的togglebutton样式 我正在将前景、背景和边框笔刷设置为SystemColor,但什么也没发生 <Style TargetType="{x:Type local:PopupButton}" BasedOn="{StaticResource {x:Type ToggleButton}}"> <Setter Property="F

我正在创建一个基于ToggleButton的自定义控件。 使用Generic.xaml中的这个简单样式,我无法在自定义控件上使用默认的togglebutton样式

我正在将前景、背景和边框笔刷设置为SystemColor,但什么也没发生

  <Style TargetType="{x:Type local:PopupButton}" BasedOn="{StaticResource {x:Type ToggleButton}}">
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
    <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.ActiveBorderBrushKey}}" />
    <Setter Property="HorizontalAlignment" Value="Center" />
    <Setter Property="VerticalAlignment" Value="Center" />
    <Setter Property="MinHeight" Value="22" />
    <Setter Property="MinWidth" Value="75" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type local:PopupButton}">
          <Grid SnapsToDevicePixels="True">
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <ToggleButton Grid.Column="0">
              <ToggleButton.Template>
                <ControlTemplate TargetType="{x:Type ToggleButton}">
                  <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                                    HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
                                    RecognizesAccessKey="True" />
                </ControlTemplate>
              </ToggleButton.Template>
              <ContentPresenter Content="{TemplateBinding Content}"
                                ContentTemplate="{TemplateBinding ContentTemplate}"
                                VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                Margin="{TemplateBinding Margin}"
                                RecognizesAccessKey="true" />
            </ToggleButton>
          </Grid>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

我一直在测试、玩弄其他(xaml)代码,但几个小时后,我仍然没有弄清楚为什么不应用默认样式。

在您的ControlTemplate for local:PopubButton中,您有一个切换按钮,但您也超越了它的模板。尝试删除:

<ToggleButton.Template>
    <ControlTemplate TargetType="{x:Type ToggleButton}">
        <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                                HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
                                RecognizesAccessKey="True" />
    </ControlTemplate>
</ToggleButton.Template>


您正在更改ControlTemplate,您希望它的外观如何保持不变。@R.Rusev我正在为ControlTemplate添加一个切换按钮,您正在覆盖它的模板。请给我一杯咖啡。将Background={TemplateBinding Background}添加到ControlTemplate中的网格中
<ToggleButton.Template>
    <ControlTemplate TargetType="{x:Type ToggleButton}">
        <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                                HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
                                RecognizesAccessKey="True" />
    </ControlTemplate>
</ToggleButton.Template>