简体   繁体   English

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

[英]GridViewColumn.CellTemplate with TextBox not working with ListView.ItemContainerStyle

I have a ListView like this: 我有一个这样的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>

This renders like shown here: 呈现如下所示:

在此处输入图片说明

As you can see, TextBox is used (I can select text), but the ListViewItemContainer gives me the glassy selection look which I don't want. 如您所见,使用了TextBox(我可以选择文本),但是ListViewItemContainer给了我我不需要的玻璃状选择外观。

Then I have defined a ItemContainerStyle (ListViewItemStyle) for my ListView which is used like that (7th row): 然后,我为我的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>

Here is my ListViewItemStyle: 这是我的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>

This gives me the selection look I want. 这给了我想要的选择外观。 But unfortunately now my TextBox template from the CellTemplate is not working anymore and the data binding isn't working either: 但是不幸的是现在我的CellTemplate中的TextBox模板不再起作用,并且数据绑定也不能起作用:

选择好,但模板和绑定不好

Happy new year! 新年快乐!

I solved it! 我解决了!

Instead of using a ContentPresenter in my Style I had to use a GridRowPresenter! 与其在我的样式中使用ContentPresenter,不如使用GridRowPresenter!

 <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>

Then it looks like this: 然后看起来像这样:

工作模板!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 GridViewColumn 或 GridViewColumn.CellTemplate 的 GridView 获取项目 - Get items from GridViewColumn or GridViewColumn.CellTemplate of GridView MultiBinding a TextBlock in a StackPanel in a GridViewColumn.CellTemplate 并且文本不显示 - MultiBinding a TextBlock in a StackPanel in a GridViewColumn.CellTemplate and the Text does not show 如何创建一个上下文菜单,其中包含Gridviewcolumn.celltemplate中的菜单项? 无法获取示例代码 - How to Create a context menu which contains menu items in a Gridviewcolumn.celltemplate? Unable to get sample code to work WPF ListView.ItemContainerStyle同时按下所有ListViewItem元素 - WPF ListView.ItemContainerStyle Presses All ListViewItem Elements Together ListView.ItemContainerStyle IsSelected属性似乎不影响WinRT上的选择 - ListView.ItemContainerStyle IsSelected property does not seem to affect selection on WinRT `ListView.ItemContainerStyle` 内的 `ContextMenu` 命令绑定不起作用 - `ContextMenu` Command binding inside `ListView.ItemContainerStyle` does not work 将文本框绑定到Listview GridViewColumn - Bind Textbox to Listview GridViewColumn 从 GridViewColumn 获取 CellTemplate 绑定 - Get CellTemplate binding from GridViewColumn 有没有一种方法可以在运行时更改GridViewColumn的CellTemplate? - Is there a way to change CellTemplate of GridViewColumn at Runtime? 如何更改用于GridViewColumn的CellTemplate的数据模板 - How to change the Datatemplate which is used for CellTemplate of the GridViewColumn
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM