单击时突出显示 WPF 列表视图中的已编程单元

发布于 2025-01-07 14:56:32 字数 1820 浏览 0 评论 0原文

我正在尝试在 WPF 中重新实现一个表单,该表单在 winforms 中相当容易编程,但看不到如何进展。该表单由三个窗格组成。右侧窗格包含详细数据 - 包含值 Y 或空白的标志列,而左侧两个窗格包含总结列之间匹配和差异的矩阵。

右侧窗格中的列数 (n) 是可变的,左侧两个窗格是 n X n - 因此它们的大小也是动态的。当用户单击左侧窗格之一中的单元格时,该单元格会突出显示,并用于突出显示右侧窗格中的相应列。

例如,考虑匹配的单元格 1,2 的情况。让我们假设它包含值 2,并且差异的 1,2 单元格包含 3。单击单元格 1,2 时,它会突出显示为绿色,差异面板上的相应单元格会突出显示为红色,并且第 1 列和第 2 列用匹配项标记,导致 2 个相应条目突出显示为绿色,3 个单个条目突出显示为红色:

                                 Column1 Column 2
                        green         Y      Y
                        red           Y
                        green         Y      Y
                        red                  Y
                        red                  Y

我决定使用列表视图在 WPF 中实现此目的。由于数组的大小可变,我决定以编程方式设置网格视图。以下是部分代码(在 VB.net 中):

Private Sub DefineColumns(ByRef _GridView As GridView, MaxIndex As Integer)
    With _GridView
        For i = 0 To MaxIndex
            Dim _ColumnTitle = "P" & (i + 1)

            Dim _TextBlock = New FrameworkElementFactory(GetType(TextBlock))
            Dim _Binding = New Binding(_ColumnTitle)
            _TextBlock.SetBinding(TextBlock.TextProperty, _Binding)
            _TextBlock.SetValue(HorizontalAlignmentProperty, Windows.HorizontalAlignment.Right)
            Dim _DataTemplate = New DataTemplate()
            _DataTemplate.VisualTree = _TextBlock

            Dim _Column = New GridViewColumn()
            With _Column
                .CellTemplate = _DataTemplate
                .Header = (i + 1)
                .Width = 39
            End With
            .Columns.Add(_Column)
        Next
    End With
End Sub

WPF 表单可以工作 - 但如何实现我需要的突出显示?我真的需要以某种方式对其进行编程,在我看来,触发器没有任何用处,因为我真的想永久标记事物,以便我可以滚动右侧窗格。我需要做的就是在左侧窗格中标记两个选定的单元格,然后在右侧窗格中循环选定的列,将匹配项标记为绿色,将差异标记为红色。有人可以帮忙吗?原理上看起来很简单,但是对于 WPF 来说,简单的事情有时实现起来会非常复杂。

I am trying to re-implement a form in WPF which was fairly easy to program in winforms and cannot see how to progress. The form consists of three panes. The right hand pane contains the detailed data - columns of flags which either contain the value Y or are blank, while the two left hand panes contain matrices which summarise the matches and discrepancies between the columns.

The number of columns (n) in the right hand pane is variable, and the two left-hand panes are n X n -so their size is dynamic too. When the user clicks a cell in one of the left hand panes, it is highlighted and used to highlight the appropriate columns in the right hand pane.

For example, consider the case of cell 1,2 of the Matches. Let us assume that it contains the value 2,and that the 1,2 cell of the Discrepancies contains 3. When the cell 1,2 is clicked, it is highlighted green,the corresponding cell on the differences panel is highlighted red, and the columns 1 and 2 are marked up with the matches, resulting in 2 corresponding entries highlighted green and 3 single entries highlighted red:

                                 Column1 Column 2
                        green         Y      Y
                        red           Y
                        green         Y      Y
                        red                  Y
                        red                  Y

I decided to use a listview to implement this in WPF. Since the arrays have variable sizes, I decided to set up the grid view programmatically. Here is part of the code (in VB.net):

Private Sub DefineColumns(ByRef _GridView As GridView, MaxIndex As Integer)
    With _GridView
        For i = 0 To MaxIndex
            Dim _ColumnTitle = "P" & (i + 1)

            Dim _TextBlock = New FrameworkElementFactory(GetType(TextBlock))
            Dim _Binding = New Binding(_ColumnTitle)
            _TextBlock.SetBinding(TextBlock.TextProperty, _Binding)
            _TextBlock.SetValue(HorizontalAlignmentProperty, Windows.HorizontalAlignment.Right)
            Dim _DataTemplate = New DataTemplate()
            _DataTemplate.VisualTree = _TextBlock

            Dim _Column = New GridViewColumn()
            With _Column
                .CellTemplate = _DataTemplate
                .Header = (i + 1)
                .Width = 39
            End With
            .Columns.Add(_Column)
        Next
    End With
End Sub

The WPF form works - but how can I achieve the highlighting I need? I really need to program it somehow and it does not seem to me that triggers are any use as I really want to mark things permanently so that I can scroll the right hand pane. All I need to do is mark the two selected cells in the left hand panes, and then loop down the selected columns on the right hand pane, marking matches in green and discrepancies in red. Can anyone help? It seems so simple in principle, but with WPF simple things can sometimes be very complicated to implement.

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

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

发布评论

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

评论(1

奢望 2025-01-14 14:56:32

我在这里没有得到任何答案,所以我决定缩小问题范围并在 MSDN 上提问。然后我设法解决这个问题。我的答案可以在这里找到:
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/6d68c606-d71e-4f89-a016-1e344d784467/#978f068f-a956-411b-b3d1-df8ac4a92cd8

I did not get any answers here so I decided to cut down the scope of my question and ask on MSDN. I then managed to program round the problem. My answer can be found here:
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/6d68c606-d71e-4f89-a016-1e344d784467/#978f068f-a956-411b-b3d1-df8ac4a92cd8

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文