WPF Selected ListViewItem:在ItemTemplate

时间:2018-06-04 11:52:00

标签: wpf xaml visual-studio-2017

我正在尝试为ListView中的Items创建一个DataTemplate,当我使用TextBlocks时,这本身就不是问题。

在ListView中选择项目时,它会按预期突出显示,背景较暗,而TextBlocks则为白色前景。

但是,当我向DataTemplate添加一个ComboBox并且突出显示一行时,无论是否选中,ComboBox的forground都保持黑色。

我没有将任何样式应用于ComboBoxes而不是SelectedValue和ItemSource属性,我没有对ComboBox做任何其他事情。

使用LivePropertyExplorer我只能看到Foreground被“覆盖”但看不到位置。

Screenshot of two items here

非常感谢帮助!

 <ListView DockPanel.Dock="Bottom"
              BorderThickness="0,1,0,0"
              Padding="3"
              BorderBrush="LightGray"
              MinHeight="150"
              MaxHeight="150"
              ItemContainerStyle="{StaticResource ListViewItemStyle25}">

        <ListView.ItemTemplate>
            <DataTemplate>
                <DockPanel LastChildFill="False">
                    <DockPanel.Resources>
                        <Style TargetType="TextBlock">
                            <Setter Property="FontSize"
                                    Value="14" />
                            <Setter Property="Background"
                                    Value="Transparent" />
                        </Style>
                    </DockPanel.Resources>

                    <ComboBox BorderThickness="1"
                              Background="Transparent"
                              MinWidth="120"
                              MaxWidth="120"
                              FontSize="14"
                              IsSynchronizedWithCurrentItem="False"
                              SelectedValue="{Binding IResourceTravelDocument_TravelDocumentType}">

                        <ComboBox.Style>
                            <Style TargetType="{x:Type ComboBox}">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding IsPrimary, ElementName=userControl}" 
                                                 Value="True">
                                        <Setter Property="ItemsSource"
                                                Value="{Binding DataContext.IResourceTravelDocumentsModuleViewModel_ResourceTravelDocumentTypes_Primary, 
                                                    ElementName=userControl}" />
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding IsPrimary, ElementName=userControl}"
                                                 Value="False">
                                        <Setter Property="ItemsSource"
                                                Value="{Binding DataContext.IResourceTravelDocumentsModuleViewModel_ResourceTravelDocumentTypes_Supplementary, 
                                                    ElementName=userControl}"  />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </ComboBox.Style>

                    </ComboBox>

                </DockPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

    <Style x:Key="ListViewItemStyle" TargetType="{x:Type ListViewItem}">

    <Setter Property="HorizontalContentAlignment"
            Value="Stretch" />
    <Setter Property="Background"
            Value="Transparent" />
    <Setter Property="VerticalContentAlignment"
            Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
    <Setter Property="Padding"
            Value="2,0,0,0" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListViewItem}">
                <Border x:Name="Bd"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        Background="{TemplateBinding Background}"
                        Padding="{TemplateBinding Padding}"
                        SnapsToDevicePixels="true">
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                      SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                </Border>
                <ControlTemplate.Triggers>

                    <Trigger Property="ItemsControl.AlternationIndex"
                             Value="0">
                        <Setter Property="Background"
                                Value="White"></Setter>
                    </Trigger>
                    <Trigger Property="ItemsControl.AlternationIndex"
                             Value="1">
                        <Setter Property="Background"
                                Value="WhiteSmoke"></Setter>
                    </Trigger>

                    <Trigger Property="IsSelected"
                             Value="true">
                        <Setter Property="Background"
                                TargetName="Bd"
                                Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
                        <Setter Property="Foreground"
                                Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" />

                    </Trigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsSelected"
                                       Value="true" />
                            <Condition Property="Selector.IsSelectionActive"
                                       Value="false" />
                        </MultiTrigger.Conditions>
                        <Setter Property="Background"
                                TargetName="Bd"
                                Value="RoyalBlue" />
                        <Setter Property="Foreground"
                                Value="AliceBlue" />
                    </MultiTrigger>
                    <Trigger Property="IsEnabled"
                             Value="false">
                        <Setter Property="Foreground"
                                Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

</Style>

1 个答案:

答案 0 :(得分:1)

ComboBox有自己的Template,默认情况下不会继承父Background的{​​{1}}和Foreground属性。

您可以将另一个ListViewItem添加到与DataTrigger绑定的ComboBox样式中:

ListViewItem