WPF组合框itemtemplate覆盖togglebutton样式

时间:2013-02-07 08:57:06

标签: c# wpf combobox styles

我对组合框的togglebutton造型有问题。 我的combobox xaml代码如下所示:

<ComboBox Width="Auto"
      HorizontalContentAlignment="Stretch"
      FontFamily="HelveticaNeue-Bold"
      FontSize="20"
      FontWeight="Bold"
      Foreground="#FFC0C0C0"
      Padding="0,0,0,0"
      Style="{DynamicResource navigationComboBox}"
      ItemsSource="{Binding Tournaments}"
      SelectedValue="{Binding SelectedTournament}">
<ComboBox.Resources>
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
</ComboBox.Resources>
<ComboBox.ItemsPanel>
    <ItemsPanelTemplate>
        <WrapPanel IsItemsHost="True" 
                    Orientation="Vertical" 
                    Width="auto"
                    Height="{Binding Tournaments, Converter={StaticResource CollectionToHeightConverter}}"/>
    </ItemsPanelTemplate>
</ComboBox.ItemsPanel>
<ComboBox.ItemTemplate>
    <DataTemplate>
        <DockPanel x:Name="comboDock">
            <DockPanel.Background>
                <ImageBrush ImageSource="{Binding Converter={StaticResource ImagePathConverter}, ConverterParameter=comboboxitem-line.png}" />
            </DockPanel.Background>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="50" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="41" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <DockPanel x:Name="comboArrow"
                            Grid.Column="0"
                            Visibility="Collapsed">
                    <DockPanel.Background>
                        <ImageBrush ImageSource="{Binding Converter={StaticResource ImagePathConverter}, ConverterParameter=SportsSubMenuActive.png}" />
                    </DockPanel.Background>
                </DockPanel>
                <TextBlock x:Name="comboText"
                            Grid.Column="1"
                            FontFamily="HelveticaNeue-Bold"
                            FontSize="20"
                            FontWeight="Bold"
                            Foreground="#FFC0C0C0"
                            Padding="0,0,10,0"
                            Text="{Binding Path=Name}"
                            TextAlignment="Left"
                            TextWrapping="Wrap" />
            </Grid>
        </DockPanel>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=ComboBoxItem}}" Value="True">
                <Setter TargetName="comboArrow" Property="Visibility" Value="Visible" />
                <Setter TargetName="comboText" Property="Foreground" Value="#F94B01" />
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>
</ComboBox.ItemTemplate>

所以我试图将背景设置删除到togglebutton,但这是不可能的。如果我删除itemtemplate上的背景,那么我可以设置切换按钮背景的样式。如果在itemtemplate中设置了togglebutton内容的背景,是否有任何特殊顺序阻止我更改togglebutton内容的背景?

先谢谢你们, 克里斯托

1 个答案:

答案 0 :(得分:1)

直接设置属性优先于任何Setter(s)。不要直接设置属性,而是为Style使用DockPanel,并使用Setter设置背景,类似于DataTrigger中的背景。这应该允许您在其他地方更改背景属性。

相关问题