在WinUI3的数据表格中禁用行选择。

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

disable row selection in winui3 datagrid

问题

我一直在努力禁用数据表格行的悬停、鼠标悬停和选择操作。以前在Wpf应用程序中,我使用了以下代码,而且它完美运行。然而,我现在正在迁移我的代码到新的WinUI 3,但却无法让它再次运行。

但问题是如何隐藏行的选择。请参考这张图片:

在WinUI3的数据表格中禁用行选择。

以下是适用于Wpf的代码;

<controls:DataGrid.Style>
   <Style TargetType="controls:DataGridCell">
           <Setter Property="BorderBrush" Value="Transparent" />
           <Setter Property="FocusVisualStyle" Value="{x:Null}" />
  </Style>
</controls:DataGrid.Style>

现在,FocusVisualStyle 不再存在。我成功地通过覆盖一些刷子资源来禁用了选择时的单元格边框,如下所示:

  <SolidColorBrush x:Key="DataGridCellFocusVisualPrimaryBrush" Color="Transparent" />
  <SolidColorBrush x:Key="DataGridCellFocusVisualSecondaryBrush" Color="Transparent" />
英文:

I have been trying very hard to disable hover, mouse over and row selections on datagrid rows. I had used the following code for Wpf application in the past and it worked perfectly. However, I am in the middle of the process of migrating my code to the new Winui3 and I just can't make it work again.

but the problem is how to hide row selections. See this picture:

在WinUI3的数据表格中禁用行选择。

Here is the code that works for Wpf;

&lt;controls:DataGrid.Style&gt;
   &lt;Style TargetType=&quot;controls:DataGridCell&quot;&gt;
           &lt;Setter Property=&quot;BorderBrush&quot; Value=&quot;Transparent&quot; /&gt;
           &lt;Setter Property=&quot;FocusVisualStyle&quot; Value=&quot;{x:Null}&quot; /&gt;
  &lt;/Style&gt;
&lt;/controls:DataGrid.Style&gt;

Now, FocusVisualStyle doesn't exists. I was able to disable cell borders on selection by overriding some brush resources like these:

  &lt;SolidColorBrush x:Key=&quot;DataGridCellFocusVisualPrimaryBrush&quot; Color=&quot;Transparent&quot; /&gt;
  &lt;SolidColorBrush x:Key=&quot;DataGridCellFocusVisualSecondaryBrush&quot; Color=&quot;Transparent&quot; /&gt;

答案1

得分: 2

这应该可以工作:

&lt;controls:DataGrid&gt;
    &lt;controls:DataGrid.Resources&gt;
        &lt;Color x:Key=&quot;DataGridRowSelectedBackgroundColor&quot;&gt;透明&lt;/Color&gt;
        &lt;Color x:Key=&quot;DataGridRowSelectedHoveredUnfocusedBackgroundColor&quot;&gt;透明&lt;/Color&gt;
        &lt;Color x:Key=&quot;DataGridRowSelectedUnfocusedBackgroundColor&quot;&gt;透明&lt;/Color&gt;
        &lt;!--
        最好不只是“透明”。
        这样,你就不会失去对悬停选定行的视觉效果。
        --&gt;
        &lt;StaticResource
            x:Key=&quot;DataGridRowSelectedHoveredBackgroundColor&quot;
            ResourceKey=&quot;SystemListLowColor&quot; /&gt;
    &lt;/controls:DataGrid.Resources&gt;
&lt;/controls:DataGrid&gt;
英文:

This should work:

&lt;controls:DataGrid&gt;
    &lt;controls:DataGrid.Resources&gt;
        &lt;Color x:Key=&quot;DataGridRowSelectedBackgroundColor&quot;&gt;Transparent&lt;/Color&gt;
        &lt;Color x:Key=&quot;DataGridRowSelectedHoveredUnfocusedBackgroundColor&quot;&gt;Transparent&lt;/Color&gt;
        &lt;Color x:Key=&quot;DataGridRowSelectedUnfocusedBackgroundColor&quot;&gt;Transparent&lt;/Color&gt;
        &lt;!--
        This one is better not being just &quot;Transparent&quot;.
        This way you won&#39;t lose visual effects for hovered selected rows.
        --&gt;
        &lt;StaticResource
            x:Key=&quot;DataGridRowSelectedHoveredBackgroundColor&quot;
            ResourceKey=&quot;SystemListLowColor&quot; /&gt;
    &lt;/controls:DataGrid.Resources&gt;
&lt;/controls:DataGrid&gt;

You can find the colors in the GitHub repo.

huangapple
  • 本文由 发表于 2023年2月19日 10:13:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497587.html
匿名

发表评论

匿名网友

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

确定