应用自定义样式后WPF模板被覆盖

4

我在app.xaml中定义了一个全局样式,用于下拉框,如下所示:

<Style x:Key="{x:Type ComboBox}" TargetType="ComboBox">
    <Setter Property="SnapsToDevicePixels" Value="true"/>
    <Setter Property="OverridesDefaultStyle" Value="true"/>
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
    <Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
    <Setter Property="MinHeight" Value="20"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ComboBox">
                <Grid Width="{TemplateBinding Width}">
                    <ToggleButton 
                                        Name="ToggleButton" 
                                        Template="{StaticResource ComboBoxToggleButton}" 
                                        Grid.Column="2" 
                                        Focusable="false"
                                        IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"
                                        ClickMode="Press">
                    </ToggleButton>
                    <ContentPresenter
                                        Name="ContentSite"
                                        IsHitTestVisible="False" 
                                        Content="{TemplateBinding SelectionBoxItem}"
                                        ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                                        ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                                        Margin="3,3,23,3"
                                        VerticalAlignment="Center"
                                        HorizontalAlignment="Left" />
                    <TextBox x:Name="PART_EditableTextBox"
                                        Style="{x:Null}" 
                                        Template="{StaticResource ComboBoxTextBox}" 
                                        HorizontalAlignment="Left" 
                                        VerticalAlignment="Center" 
                                        Margin="3,3,23,3"
                                        Focusable="True" 
                                        Background="Transparent"
                                        Visibility="Hidden"
                                        IsReadOnly="{TemplateBinding IsReadOnly}"/>
                    <Popup 
                                        Name="Popup"
                                        Placement="Bottom"
                                        IsOpen="{TemplateBinding IsDropDownOpen}"
                                        AllowsTransparency="True" 
                                        Focusable="False"
                                        PopupAnimation="Slide">
                        <Grid 
                                            Name="DropDown"
                                            SnapsToDevicePixels="True"                
                                            MinWidth="{TemplateBinding ActualWidth}"
                                            MaxHeight="{TemplateBinding MaxDropDownHeight}">
                            <Border x:Name="DropDownBorder" Background="{StaticResource BackgroundBrush}" />
                            <ScrollViewer Margin="4,4,4,4" SnapsToDevicePixels="True" Style="{StaticResource DropDownScrollViewer}">
                                <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
                            </ScrollViewer>
                        </Grid>
                    </Popup>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="HasItems" Value="false">
                        <Setter TargetName="DropDownBorder" Property="MinHeight" Value="95"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
                    </Trigger>
                    <Trigger Property="IsGrouping" Value="true">
                        <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                    </Trigger>
                    <Trigger Property="IsEditable" Value="true">
                        <Setter Property="IsTabStop" Value="false"/>
                        <Setter TargetName="PART_EditableTextBox" Property="Visibility" Value="Visible"/>
                        <Setter TargetName="ContentSite" Property="Visibility" Value="Hidden"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

然后我在一个控件中创建了一个下拉框:

<ComboBox Name="DataTypeSelector" ItemsSource="{Binding ElementName=DataItemsBuildWindow, Path=DataContext.Types}" SelectedValue="{Binding DataType}" HorizontalAlignment="Stretch" />                                      

样式按预期应用。

如果我将控件中的组合框更改为以下内容,则组合框会恢复其原始样式,并且已定义的宽度和触发器有效。似乎新的全局样式被忽略了。

<ComboBox Name="DataTypeSelector" ItemsSource="{Binding ElementName=DataItemsBuildWindow, Path=DataContext.Types}" SelectedValue="{Binding DataType}" HorizontalAlignment="Stretch">
                                    <ComboBox.Style>
                                        <Style TargetType="ComboBox">
                                            <Setter Property="Width" Value="160"></Setter>
                                            <Style.Triggers>
                                                <DataTrigger Value="List" Binding="{Binding SelectedValue, ElementName=DataTypeSelector, Converter={StaticResource ToStringConverter}}">
                                                    <Setter Property="Width" Value="80" />
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </ComboBox.Style>
                                </ComboBox>

如何保持全局样式并应用自定义触发器?
1个回答

14

你需要以现有的默认样式为基础来制定新的样式:

<ComboBox.Style>
    <Style TargetType="ComboBox"
           BasedOn="{StaticResource ResourceKey={x:Type ComboBox}}">
        ...
    </Style>
</ComboBox.Style>
顺便提一下,在App.xaml中的资源字典里设置默认样式的键名并不是必须的。如果你指定了TargetType="ComboBox",那么键名会默认设置为该类型。因此,以下代码已经足够了:
<Style TargetType="ComboBox">
    ...
</Style>

谢谢,那个起作用了。有没有一种方法可以完全覆盖我的应用程序的默认样式,这样我就不必这样做了? - VARAK
1
不确定你的意思。你已经为 ComboBox 设置了应用程序范围内的默认样式,还需要覆盖什么? - Clemens
对,这很有道理...我还在摸索WPF样式方面的知识:) 谢谢! - VARAK

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接