英文:
Disable highlighting on side of active ListViewItem in WinUI
问题
我正在使用WinUI 3,并且有一个包含文本行的列表视图。每一行右对齐有两个按钮。
每当我点击一个项目时,它会在一侧突出显示。
如何禁用这个突出显示?我尝试将ListViewItem的BorderBrush设置为透明,这解决了问题,但却破坏了按钮的对齐:
<ListView>
<!-- ... -->
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="BorderBrush" Value="Transparent" />
</Style>
</ListView.ItemContainerStyle>
<!-- ... -->
</ListView>
<details>
<summary>英文:</summary>
I'm using WinUI 3 and have a list view with rows of text. Each row has 2 buttons right aligned.
```xaml
<ListView>
<ListView.ItemTemplate>
<DataTemplate x:DataType="x:String">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{x:Bind}" Grid.Column="0"/>
<Button Grid.Column="1" Content="Button 1"/>
<Button Grid.Column="2" Content="Button 2"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
<x:String>Hello!</x:String>
<x:String>Hello!</x:String>
<x:String>Hello!</x:String>
</ListView>
Whenever I click on an item it gets a highlight on the side.
How do I disable the highlight? I've tried setting the BorderBrush
of the ListViewItem
to be transparent and that fixes it, but it ruins the alignment of the buttons:
<ListView>
...
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="BorderBrush" Value="Transparent" />
</Style>
</ListView.ItemContainerStyle>
...
</ListView>
答案1
得分: 1
如果您只想删除选择指示器,您可以覆盖ListViewItemSelectionIndicatorVisualEnabled
并将其设置为False
:
<ListView>
<ListView.Resources>
<x:Boolean x:Key="ListViewItemSelectionIndicatorVisualEnabled">False</x:Boolean>
</ListView.Resources>
...
</ListView>
英文:
If you just want to remove the selection indicator, you can just override ListViewItemSelectionIndicatorVisualEnabled
and set it to False
:
<ListView>
<ListView.Resources>
<x:Boolean x:Key="ListViewItemSelectionIndicatorVisualEnabled">False</x:Boolean>
</ListView.Resources>
...
</ListView>
答案2
得分: 0
通过将 BorderBrush
设置为透明并将 HorizontalContentAlignment
设置为 stretch 来修复问题。
<ListView>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="BorderBrush" Value="Transparent" />
<!-- 这将强制网格重新拉伸 -->
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate x:DataType="x:String">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{x:Bind}" Grid.Column="0"/>
<Button Grid.Column="1" Content="Button 1"/>
<Button Grid.Column="2" Content="Button 2"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
<x:String>Hello!</x:String>
<x:String>Hello!</x:String>
<x:String>Hello!</x:String>
</ListView>
英文:
Fixed by setting BorderBrush
to transparent and HorizontalContentAlignment
to stretch.
<ListView>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="BorderBrush" Value="Transparent" />
<!-- This forces the grid to stretch back out -->
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate x:DataType="x:String">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{x:Bind}" Grid.Column="0"/>
<Button Grid.Column="1" Content="Button 1"/>
<Button Grid.Column="2" Content="Button 2"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
<x:String>Hello!</x:String>
<x:String>Hello!</x:String>
<x:String>Hello!</x:String>
</ListView>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论