为什么 ContentControl 中的 ListViewItems 只能在前几个像素上可选?
Why are ListViewItems within ContentControl only selectable on first few pixels?
我想向我的 WPF 应用程序的用户请求不同类型的属性。因此我有一个 ListView
那就是 ItemsSource
绑定到一个 ObservableCollection<PropertiesBase>
。每个 属性 派生自 PropertiesBase
。作为专家用户应该能够编辑这些属性,DataTemplate
是 selected 取决于 EditMode
属性 和 属性 的类型。
数据显示和其他一切正常,除了我无法 select ListViewItem。 只有当我单击 [=18= 中的文本框时] 或项目的前几个像素我能够 select 一个项目。
The selection only works on the white part of the item
我尝试使用 Focusable,但它并没有让我成功。我还将 ListViewItem 的 xaml 直接复制到 Listview 中(没有数据模板)。效果如预期。
列表视图XAML:
<ListView ItemsSource="{Binding PropertyList}" HorizontalContentAlignment="Stretch" SelectionMode="Single">
<ListView.Resources>
<DataTemplate DataType="{x:Type properties:PasswordProperty}">
<ContentControl Content="{Binding}" Background="Red">
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Setter Property="ContentTemplate" Value="{StaticResource PasswordPropertyListViewItem}" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=DataContext.EditMode, RelativeSource= {RelativeSource FindAncestor, AncestorType={x:Type ListView}}}" Value="True">
<Setter Property="ContentTemplate" Value="{StaticResource EditPasswordPropertyListViewItem}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</DataTemplate>
DataTemplate 引用的示例 ListViewItem XAML:
<ListViewItem x:Class="PasswordPropertyListViewItem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008">
<Border BorderThickness="0,0,0,1" BorderBrush="DarkGray">
<DockPanel LastChildFill="True" HorizontalAlignment="Stretch">
<Label Margin="8" Content="{Binding PropertyName}"></Label>
我希望在 ListViewItem 的哪个位置单击并不重要,但在任何情况下都会 select 编辑该项目(尤其是上图中的红色部分)。
Clemens provided a solution in his
我确实嵌套了两个 ListViewItems
.
解决方案是将我的模板化控件的类型更改为 ContentControl
。
<ContentControl x:Class="PasswordPropertyListViewItem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008">
<Border BorderThickness="0,0,0,1" BorderBrush="DarkGray">
<DockPanel LastChildFill="True" HorizontalAlignment="Stretch">
<Label Margin="8" Content="{Binding PropertyName}"></Label>
谢谢克莱门斯!
我想向我的 WPF 应用程序的用户请求不同类型的属性。因此我有一个 ListView
那就是 ItemsSource
绑定到一个 ObservableCollection<PropertiesBase>
。每个 属性 派生自 PropertiesBase
。作为专家用户应该能够编辑这些属性,DataTemplate
是 selected 取决于 EditMode
属性 和 属性 的类型。
数据显示和其他一切正常,除了我无法 select ListViewItem。 只有当我单击 [=18= 中的文本框时] 或项目的前几个像素我能够 select 一个项目。
The selection only works on the white part of the item
我尝试使用 Focusable,但它并没有让我成功。我还将 ListViewItem 的 xaml 直接复制到 Listview 中(没有数据模板)。效果如预期。
列表视图XAML:
<ListView ItemsSource="{Binding PropertyList}" HorizontalContentAlignment="Stretch" SelectionMode="Single">
<ListView.Resources>
<DataTemplate DataType="{x:Type properties:PasswordProperty}">
<ContentControl Content="{Binding}" Background="Red">
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Setter Property="ContentTemplate" Value="{StaticResource PasswordPropertyListViewItem}" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=DataContext.EditMode, RelativeSource= {RelativeSource FindAncestor, AncestorType={x:Type ListView}}}" Value="True">
<Setter Property="ContentTemplate" Value="{StaticResource EditPasswordPropertyListViewItem}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</DataTemplate>
DataTemplate 引用的示例 ListViewItem XAML:
<ListViewItem x:Class="PasswordPropertyListViewItem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008">
<Border BorderThickness="0,0,0,1" BorderBrush="DarkGray">
<DockPanel LastChildFill="True" HorizontalAlignment="Stretch">
<Label Margin="8" Content="{Binding PropertyName}"></Label>
我希望在 ListViewItem 的哪个位置单击并不重要,但在任何情况下都会 select 编辑该项目(尤其是上图中的红色部分)。
Clemens provided a solution in his ListViewItems
.
解决方案是将我的模板化控件的类型更改为 ContentControl
。
<ContentControl x:Class="PasswordPropertyListViewItem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008">
<Border BorderThickness="0,0,0,1" BorderBrush="DarkGray">
<DockPanel LastChildFill="True" HorizontalAlignment="Stretch">
<Label Margin="8" Content="{Binding PropertyName}"></Label>
谢谢克莱门斯!