WPF DataGrid 超链接的外观和行为

发布于 2024-11-18 19:19:18 字数 2715 浏览 2 评论 0原文

我对 WPF 还很陌生,有很多东西需要学习,我想我正在慢慢地到达那里。我有一个用于显示和用户输入的 DataGrid,它是一个非常棘手的网格,因为它是整个应用程序的主要焦点。我有一些只读的列,并且我使用 CellStyle Setter 将 KeyboardNavigation.IsTabStop 设置为 False,以使用户输入集中在重要的列上,并且效果很好。我希望几个只读列成为超链接,显示工具提示并且不接收焦点,但是我正在努力编写同时满足所有三个要求的 XAML。

其中一列用于指示该行上的项目是否有任何注释。我使用以下 XAML 在 DataGridTemplateColumn 的单元格中显示 HasNotes 属性,并在工具提示上显示 Notes 属性中的实际注释:

            <DataGridTemplateColumn x:Name="NotesColumn" Header="Notes">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding HasNotes, Mode=OneWay}">
                            <TextBlock.ToolTip>
                                <TextBlock Text="{Binding Notes}" MaxWidth="300" TextWrapping="Wrap" />
                            </TextBlock.ToolTip>
                        </TextBlock>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellStyle>
                    <Style>
                        <Setter Property="KeyboardNavigation.IsTabStop" Value="False"/>
                    </Style>
                </DataGridTemplateColumn.CellStyle>
            </DataGridTemplateColumn>

效果很好,但我想将其设为超链接,以便用户可以执行某些操作当他们单击单元格内容时,会带有注释。

我还有另一列,它是 DataGridHyperlinkColumn,用于在超链接上显示测量单位,单击时用户可以更改单位。 (我之所以这样做,而不是像 ComboBox 那样,是因为尽管我希望用户能够更改单位,但我希望使界面成为一种非常有意的行为,以更改单位,而不是偶然完成的事情)。以下 XAML 将超链接列置于

            <DataGridHyperlinkColumn x:Name="ResultUnitLink" Binding="{Binding Path=Unit.Code}" Header="Unit" Width="Auto" IsReadOnly="True">
                <DataGridHyperlinkColumn.CellStyle>
                    <Style>
                        <Setter Property="KeyboardNavigation.IsTabStop" Value="False"/>
                    </Style>
                </DataGridHyperlinkColumn.CellStyle>
                <DataGridHyperlinkColumn.ElementStyle>
                    <Style>
                        <EventSetter Event="Hyperlink.Click" Handler="ChangeUnit" />
                    </Style>
                </DataGridHyperlinkColumn.ElementStyle>
            </DataGridHyperlinkColumn>

第一单元,超链接列的 XAML 的问题是 IsTabStop = False 似乎不起作用,当通过网格进行 Tab 键切换时,我的超链接列仍然获得焦点,这与其他列不同,其中我使用了 setter 来更改 IsTabStop。如果到了紧要关头,我可以忍受,但我不愿意。

我实际上想要从这两列中得到的是两种外观/行为的合并,即数据显示在超链接上的列,其中 TabStop = False ,并且当鼠标悬停在列上时显示不同属性的工具提示。

任何人都可以帮助我如何获得一个列来实现以下功能:

  1. 超链接显示一个属性
  2. 工具提示显示不同的属性
  3. IsTabStop = False 与超链接一起使用时实际上有效

提前感谢任何可以提供帮助的人。

I am quite new to WPF, there is so much to learn and I think I'm getting there, slowly. I have a DataGrid which is used for display and user input, it's quite a tricky Grid as it is the main focus of the entire application. I have some columns that are read-only and I have used a CellStyle Setter to set KeyboardNavigation.IsTabStop to False to keep user input focused on the important columns and that works fine. I would like a couple of the read-only columns to be hyperlinks that show a tooltip and do not receive the focus, however I am struggling to write the XAML that will achieve all three requirements at the same time.

One of the columns is to indicate if the item on the row has any Notes. I have used the following XAML to display a HasNotes property in the cell in a DataGridTemplateColumn and on the Tooltip show the actual notes, in the Notes property:

            <DataGridTemplateColumn x:Name="NotesColumn" Header="Notes">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding HasNotes, Mode=OneWay}">
                            <TextBlock.ToolTip>
                                <TextBlock Text="{Binding Notes}" MaxWidth="300" TextWrapping="Wrap" />
                            </TextBlock.ToolTip>
                        </TextBlock>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellStyle>
                    <Style>
                        <Setter Property="KeyboardNavigation.IsTabStop" Value="False"/>
                    </Style>
                </DataGridTemplateColumn.CellStyle>
            </DataGridTemplateColumn>

That works fine but I would like to make it a Hyperlink instead so the user can do something with the Notes when they click on the cell contents.

I have another column which is a DataGridHyperlinkColumn used to display a Unit of Measurement on a hyperlink and when clicked the user can change the unit. (The reason I have done it like this, rather than a ComboBox for example, is because as much as I want the user to be able to change the unit I want to make the interface such that it is a very deliberate act to change the unit, not something that can be done accidentally). The following XAML puts the Hyperlink column on for Unit

            <DataGridHyperlinkColumn x:Name="ResultUnitLink" Binding="{Binding Path=Unit.Code}" Header="Unit" Width="Auto" IsReadOnly="True">
                <DataGridHyperlinkColumn.CellStyle>
                    <Style>
                        <Setter Property="KeyboardNavigation.IsTabStop" Value="False"/>
                    </Style>
                </DataGridHyperlinkColumn.CellStyle>
                <DataGridHyperlinkColumn.ElementStyle>
                    <Style>
                        <EventSetter Event="Hyperlink.Click" Handler="ChangeUnit" />
                    </Style>
                </DataGridHyperlinkColumn.ElementStyle>
            </DataGridHyperlinkColumn>

One problem with the XAML for the Hyperlink column is that the IsTabStop = False doesn't appear to work, when tabbing through the Grid my hyperlink column still receives the focus, unlike the other columns where I've used a setter to change IsTabStop. If push comes to shove I could live with that but I'd rather not.

What I actually want from both those columns is an amalgamation of the two appearances/behaviours i.e. Columns where the data is displayed on a hyperlink, where TabStop = False and which display a tooltip of a different property when hovered over.

Can anyone help advise me how to get a column that achieves the following:

  1. Hyperlink displaying one property
  2. Tooltip displaying a different property
  3. IsTabStop = False that actually works when used with a hyperlink

Thanks in advance to anyone who can help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨落画卷 2024-11-25 19:19:18

我过去曾遇到过超链接问题,因此我将这种样式用于标签或按钮,使它们看起来像超链接。尝试将您的列模板制作为按钮或标签并应用此样式。

<Style x:Key="ButtonAsLinkStyle" TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <TextBlock HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                    <ContentPresenter ContentStringFormat="{TemplateBinding ContentStringFormat}" />
                </TextBlock>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Foreground" Value="Blue" />
    <Setter Property="Cursor" Value="Hand" />
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Foreground" Value="Red" />
        </Trigger>
    </Style.Triggers>
</Style>

I've had issues with the Hyperlink in the past, so have this Style which I use for Labels or Buttons to make them look like Hyperlinks. Try making your column Template into a Button or a Label and applying this style.

<Style x:Key="ButtonAsLinkStyle" TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <TextBlock HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                    <ContentPresenter ContentStringFormat="{TemplateBinding ContentStringFormat}" />
                </TextBlock>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Foreground" Value="Blue" />
    <Setter Property="Cursor" Value="Hand" />
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Foreground" Value="Red" />
        </Trigger>
    </Style.Triggers>
</Style>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文