Wpf ListViewItem自定义控件没有';t对仍属于项目一部分的某些空白区域中的单击不作出响应
我正在尝试为继承自ListView的控件和继承自ListViewItem的控件创建自定义控件。外观很好。问题是,当单击下面显示的控件部分时,该项目没有被选中-您必须直接单击图像。文本的边距也有同样的问题(你不能点击边距),但我给它们添加了填充。我原以为在Wpf ListViewItem自定义控件没有';t对仍属于项目一部分的某些空白区域中的单击不作出响应,wpf,xaml,custom-controls,Wpf,Xaml,Custom Controls,我正在尝试为继承自ListView的控件和继承自ListViewItem的控件创建自定义控件。外观很好。问题是,当单击下面显示的控件部分时,该项目没有被选中-您必须直接单击图像。文本的边距也有同样的问题(你不能点击边距),但我给它们添加了填充。我原以为在ContentPresenter.content中添加内容可以解决问题,但事实并非如此 另外,如果我将此样式添加到我的资源中,而不是在Generic.xaml中,并删除NavigationListViewItem.cs中的DefaultStyl
ContentPresenter.content中添加内容可以解决问题,但事实并非如此
另外,如果我将此样式添加到我的资源中,而不是在Generic.xaml中,并删除NavigationListViewItem.cs
中的DefaultStyleKeyProperty.OverrideMetadata
方法,它确实可以正常工作(不确定上面有什么)
自定义控件的代码隐藏没有添加任何附加代码。以下是Generic.xaml中的样式:
<Style TargetType="{x:Type ui:NavigationListViewItem}">
<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="Foreground" Value="White" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ui:NavigationListViewItem}">
<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}">
<ContentPresenter.Content>
<StackPanel x:Name="itemPanel"
MaxWidth="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}, Path=ActualWidth}"
VerticalAlignment="Center">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="19" />
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition MinHeight="22" />
</Grid.RowDefinitions>
<ui:ColorableImage Grid.Column="0" Grid.Row="0" Source="arrow-right-white.png" Height="15" Width="15" />
<TextBlock Grid.Column="1" Grid.Row="0"
x:Name="itemText"
FontSize="14"
FontFamily="Segoe UI"
FontWeight="Bold"
TextWrapping="Wrap"
VerticalAlignment="Center"
HorizontalAlignment="Stretch"
Text="Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text "
Padding="3,10,10,10" />
</Grid>
</StackPanel>
</ContentPresenter.Content>
</ContentPresenter>
</Border>
<ControlTemplate.Triggers>
<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}}"/>
<Setter TargetName="itemText" Property="Foreground" Value="Red" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="true"/>
<Condition Property="Selector.IsSelectionActive" Value="false"/>
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}}"/>
</MultiTrigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
默认情况下,导航列表视图项
的边框
背景设置为空,不会对鼠标单击做出反应。您需要将其初始化为某些内容,例如透明
<Style TargetType="{x:Type ui:NavigationListViewItem}">
<!-- other setters -->
<Setter Property="Foreground" Value="White" />
<Setter Property="Background" Value="Transparent" />
事情总是很简单。。。另外,如果边界存在,那么边界背景是否为空又有什么关系呢?边框是否有背景似乎不应该确定用户是否可以单击它。如果background
笔刷未初始化,则它不可见。这就是它的工作原理,在某些情况下可能是有用的。如果你想让你的控件在整个控件区域都能被点击测试,那么你需要初始化Background
。好吧,我想这是你必须知道的。我想知道为什么默认情况下后台没有初始化为透明。编辑:实际上,不管怎样,对于代码隐藏来说,检查它是否已初始化是有意义的。。。对于XAML来说,这有点烦人。它被初始化为默认值,而对于后台
和其他引用类型,默认值将为空。它只是没有初始化为画笔的某个实例DependencyProperties
共享默认值,因此如果该值不为null,则所有Control.Background
属性将共享对笔刷的同一实例的引用,这可能会有问题
<Trigger Property="IsSelected" Value="true">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
<!-- other setters -->
</Trigger>