GridViewColumn.CellTemplate TextBox 无法与 ListView.ItemContainerStyle 一起使用

GridViewColumn.CellTemplate with TextBox not working with ListView.ItemContainerStyle

我有一个这样的 ListView:

      <ListView ItemsSource="{Binding Components}"
                      BorderThickness="0"
                      Margin="0,2,0,0"
                      HorizontalAlignment="Stretch"
                      MinHeight="150"
                      SelectionMode="Single"
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="Component Name">
                            <GridViewColumn.CellTemplate>

                                <DataTemplate DataType="{x:Type viewModels:ComponentViewModel}">
                                    <TextBox Text="{Binding Name}"
                                             Style="{StaticResource TextBoxInListViewStyle}">
                                    </TextBox>
                                </DataTemplate>

                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>

呈现如下所示:

如您所见,使用了 TextBox(我可以 select 文本),但 ListViewItemContainer 给了我玻璃般的 selection 外观,这是我不想要的。

然后我为我的 ListView 定义了一个 ItemContainerStyle (ListViewItemStyle),它是这样使用的(第 7 行):

      <ListView ItemsSource="{Binding Components}"
                      BorderThickness="0"
                      Margin="0,2,0,0"
                      HorizontalAlignment="Stretch"
                      MinHeight="150"
                      SelectionMode="Single"
                      ItemContainerStyle="{StaticResource ListViewItemStyle}"
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="Component Name">
                            <GridViewColumn.CellTemplate>

                                <DataTemplate DataType="{x:Type viewModels:ComponentViewModel}">
                                    <TextBox Text="{Binding Name}"
                                             Style="{StaticResource TextBoxInListViewStyle}">
                                    </TextBox>
                                </DataTemplate>

                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>

这是我的 ListViewItemStyle:

<Style x:Key="ListViewItemStyle"
       TargetType="{x:Type ListViewItem}">
    <Setter Property="SnapsToDevicePixels"
            Value="True" />
    <Setter Property="Padding"
            Value="4,1" />
    <Setter Property="HorizontalContentAlignment"
            Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
    <Setter Property="VerticalContentAlignment"
            Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
    <Setter Property="Background"
            Value="Transparent" />
    <Setter Property="BorderBrush"
            Value="Transparent" />
    <Setter Property="BorderThickness"
            Value="1" />
    <Setter Property="FocusVisualStyle"
            Value="{StaticResource FocusVisual}" />
    <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}"
                                      ContentTemplate="{TemplateBinding ContentTemplate}" />
                </Border>
                <ControlTemplate.Triggers>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsMouseOver"
                                       Value="True" />
                        </MultiTrigger.Conditions>
                        <Setter Property="Background"
                                TargetName="Bd"
                                Value="{StaticResource Item.MouseOver.Background}" />
                        <Setter Property="BorderBrush"
                                TargetName="Bd"
                                Value="{StaticResource Item.MouseOver.Border}" />
                    </MultiTrigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="Selector.IsSelectionActive"
                                       Value="False" />
                            <Condition Property="IsSelected"
                                       Value="True" />
                        </MultiTrigger.Conditions>
                        <Setter Property="Background"
                                TargetName="Bd"
                                Value="{StaticResource Item.SelectedInactive.Background}" />
                        <Setter Property="BorderBrush"
                                TargetName="Bd"
                                Value="{StaticResource Item.SelectedInactive.Border}" />
                    </MultiTrigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="Selector.IsSelectionActive"
                                       Value="True" />
                            <Condition Property="IsSelected"
                                       Value="True" />
                        </MultiTrigger.Conditions>
                        <Setter Property="Background"
                                TargetName="Bd"
                                Value="{StaticResource Item.SelectedActive.Background}" />
                        <Setter Property="BorderBrush"
                                TargetName="Bd"
                                Value="{StaticResource Item.SelectedActive.Border}" />
                    </MultiTrigger>
                    <Trigger Property="IsEnabled"
                             Value="False">
                        <Setter Property="TextElement.Foreground"
                                TargetName="Bd"
                                Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这给了我想要的 selection 外观。但不幸的是,现在我的 CellTemplate 中的 TextBox 模板不再起作用,数据绑定也不起作用:

新年快乐!

我解决了!

我不得不使用 GridRowPresenter,而不是在我的样式中使用 ContentPresenter!

 <Style x:Key="ListViewItemStyle"
       TargetType="{x:Type ListViewItem}">
    <Setter Property="SnapsToDevicePixels"
            Value="True" />
    <Setter Property="Padding"
            Value="4,1" />
    <Setter Property="HorizontalContentAlignment"
            Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
    <Setter Property="VerticalContentAlignment"
            Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
    <Setter Property="Background"
            Value="Transparent" />
    <Setter Property="BorderBrush"
            Value="Transparent" />
    <Setter Property="BorderThickness"
            Value="1" />
    <Setter Property="FocusVisualStyle"
            Value="{StaticResource FocusVisual}" />
    <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">
                    <GridViewRowPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                      SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                       />
                </Border>
                <ControlTemplate.Triggers>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsMouseOver"
                                       Value="True" />
                        </MultiTrigger.Conditions>
                        <Setter Property="Background"
                                TargetName="Bd"
                                Value="{StaticResource Item.MouseOver.Background}" />
                        <Setter Property="BorderBrush"
                                TargetName="Bd"
                                Value="{StaticResource Item.MouseOver.Border}" />
                    </MultiTrigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="Selector.IsSelectionActive"
                                       Value="False" />
                            <Condition Property="IsSelected"
                                       Value="True" />
                        </MultiTrigger.Conditions>
                        <Setter Property="Background"
                                TargetName="Bd"
                                Value="{StaticResource Item.SelectedInactive.Background}" />
                        <Setter Property="BorderBrush"
                                TargetName="Bd"
                                Value="{StaticResource Item.SelectedInactive.Border}" />
                    </MultiTrigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="Selector.IsSelectionActive"
                                       Value="True" />
                            <Condition Property="IsSelected"
                                       Value="True" />
                        </MultiTrigger.Conditions>
                        <Setter Property="Background"
                                TargetName="Bd"
                                Value="{StaticResource Item.SelectedActive.Background}" />
                        <Setter Property="BorderBrush"
                                TargetName="Bd"
                                Value="{StaticResource Item.SelectedActive.Border}" />
                    </MultiTrigger>
                    <Trigger Property="IsEnabled"
                             Value="False">
                        <Setter Property="TextElement.Foreground"
                                TargetName="Bd"
                                Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

然后看起来像这样: