在WinUI中禁用活动ListViewItem侧边的高亮显示。

huangapple go评论90阅读模式
英文:

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>

在WinUI中禁用活动ListViewItem侧边的高亮显示。


<details>
<summary>英文:</summary>

I&#39;m using WinUI 3 and have a list view with rows of text. Each row has 2 buttons right aligned. 
```xaml
&lt;ListView&gt;
    &lt;ListView.ItemTemplate&gt;
        &lt;DataTemplate x:DataType=&quot;x:String&quot;&gt;
            &lt;Grid&gt;
                &lt;Grid.ColumnDefinitions&gt;
                    &lt;ColumnDefinition Width=&quot;*&quot;/&gt;
                    &lt;ColumnDefinition Width=&quot;Auto&quot;/&gt;
                    &lt;ColumnDefinition Width=&quot;Auto&quot;/&gt;
                &lt;/Grid.ColumnDefinitions&gt;
                &lt;TextBlock Text=&quot;{x:Bind}&quot; Grid.Column=&quot;0&quot;/&gt;
                &lt;Button Grid.Column=&quot;1&quot; Content=&quot;Button 1&quot;/&gt;
                &lt;Button Grid.Column=&quot;2&quot; Content=&quot;Button 2&quot;/&gt;
            &lt;/Grid&gt;
        &lt;/DataTemplate&gt;
    &lt;/ListView.ItemTemplate&gt;
    &lt;x:String&gt;Hello!&lt;/x:String&gt;
    &lt;x:String&gt;Hello!&lt;/x:String&gt;
    &lt;x:String&gt;Hello!&lt;/x:String&gt;
&lt;/ListView&gt;

Whenever I click on an item it gets a highlight on the side.
在WinUI中禁用活动ListViewItem侧边的高亮显示。
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:

&lt;ListView&gt;
   ...
    &lt;ListView.ItemContainerStyle&gt;
        &lt;Style TargetType=&quot;ListViewItem&quot;&gt;
            &lt;Setter Property=&quot;BorderBrush&quot; Value=&quot;Transparent&quot; /&gt;
        &lt;/Style&gt;
    &lt;/ListView.ItemContainerStyle&gt;
   ...
&lt;/ListView&gt;

在WinUI中禁用活动ListViewItem侧边的高亮显示。

答案1

得分: 1

如果您只想删除选择指示器,您可以覆盖ListViewItemSelectionIndicatorVisualEnabled并将其设置为False

&lt;ListView&gt;
    &lt;ListView.Resources&gt;
        &lt;x:Boolean x:Key=&quot;ListViewItemSelectionIndicatorVisualEnabled&quot;&gt;False&lt;/x:Boolean&gt;
    &lt;/ListView.Resources&gt;
...
&lt;/ListView&gt;
英文:

If you just want to remove the selection indicator, you can just override ListViewItemSelectionIndicatorVisualEnabled and set it to False:

&lt;ListView&gt;
    &lt;ListView.Resources&gt;
        &lt;x:Boolean x:Key=&quot;ListViewItemSelectionIndicatorVisualEnabled&quot;&gt;False&lt;/x:Boolean&gt;
    &lt;/ListView.Resources&gt;
...
&lt;/ListView&gt;

答案2

得分: 0

通过将 BorderBrush 设置为透明并将 HorizontalContentAlignment 设置为 stretch 来修复问题。

&lt;ListView&gt;
    &lt;ListView.ItemContainerStyle&gt;
        &lt;Style TargetType=&quot;ListViewItem&quot;&gt;
            &lt;Setter Property=&quot;BorderBrush&quot; Value=&quot;Transparent&quot; /&gt;
            &lt;!-- 这将强制网格重新拉伸 --&gt;
            &lt;Setter Property=&quot;HorizontalContentAlignment&quot; Value=&quot;Stretch&quot; /&gt;
        &lt;/Style&gt;
    &lt;/ListView.ItemContainerStyle&gt;
    &lt;ListView.ItemTemplate&gt;
        &lt;DataTemplate x:DataType=&quot;x:String&quot;&gt;
            &lt;Grid&gt;
                &lt;Grid.ColumnDefinitions&gt;
                    &lt;ColumnDefinition Width=&quot;*&quot;/&gt;
                    &lt;ColumnDefinition Width=&quot;Auto&quot;/&gt;
                    &lt;ColumnDefinition Width=&quot;Auto&quot;/&gt;
                &lt;/Grid.ColumnDefinitions&gt;
                &lt;TextBlock Text=&quot;{x:Bind}&quot; Grid.Column=&quot;0&quot;/&gt;
                &lt;Button Grid.Column=&quot;1&quot; Content=&quot;Button 1&quot;/&gt;
                &lt;Button Grid.Column=&quot;2&quot; Content=&quot;Button 2&quot;/&gt;
            &lt;/Grid&gt;
        &lt;/DataTemplate&gt;
    &lt;/ListView.ItemTemplate&gt;
    &lt;x:String&gt;Hello!&lt;/x:String&gt;
    &lt;x:String&gt;Hello!&lt;/x:String&gt;
    &lt;x:String&gt;Hello!&lt;/x:String&gt;
&lt;/ListView&gt;
英文:

Fixed by setting BorderBrush to transparent and HorizontalContentAlignment to stretch.

&lt;ListView&gt;
    &lt;ListView.ItemContainerStyle&gt;
        &lt;Style TargetType=&quot;ListViewItem&quot;&gt;
            &lt;Setter Property=&quot;BorderBrush&quot; Value=&quot;Transparent&quot; /&gt;
            &lt;!-- This forces the grid to stretch back out --&gt;
            &lt;Setter Property=&quot;HorizontalContentAlignment&quot; Value=&quot;Stretch&quot; /&gt;
        &lt;/Style&gt;
    &lt;/ListView.ItemContainerStyle&gt;
    &lt;ListView.ItemTemplate&gt;
        &lt;DataTemplate x:DataType=&quot;x:String&quot;&gt;
            &lt;Grid&gt;
                &lt;Grid.ColumnDefinitions&gt;
                    &lt;ColumnDefinition Width=&quot;*&quot;/&gt;
                    &lt;ColumnDefinition Width=&quot;Auto&quot;/&gt;
                    &lt;ColumnDefinition Width=&quot;Auto&quot;/&gt;
                &lt;/Grid.ColumnDefinitions&gt;
                &lt;TextBlock Text=&quot;{x:Bind}&quot; Grid.Column=&quot;0&quot;/&gt;
                &lt;Button Grid.Column=&quot;1&quot; Content=&quot;Button 1&quot;/&gt;
                &lt;Button Grid.Column=&quot;2&quot; Content=&quot;Button 2&quot;/&gt;
            &lt;/Grid&gt;
        &lt;/DataTemplate&gt;
    &lt;/ListView.ItemTemplate&gt;
    &lt;x:String&gt;Hello!&lt;/x:String&gt;
    &lt;x:String&gt;Hello!&lt;/x:String&gt;
    &lt;x:String&gt;Hello!&lt;/x:String&gt;
&lt;/ListView&gt;

huangapple
  • 本文由 发表于 2023年6月13日 06:33:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460699.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定