简体   繁体   English

如何在WPF Datagrid ColumnHeader中找到图像,以便可以更改图像?

[英]How do I find the image in a WPF Datagrid ColumnHeader so I can change the image?

I'm attempting to implement Excel-like column filtering and sorting. 我正在尝试实现类似Excel的列过滤和排序。 To do this, I used a DataTemplate to define the Column Header. 为此,我使用了一个DataTemplate来定义列标题。

    <DataGrid x:Name="dataGrid" HorizontalAlignment="Stretch"  VerticalAlignment="Stretch" CanUserSortColumns="False">
        <DataGrid.Resources>
            <Style TargetType="{x:Type DataGridColumnHeader}">
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="23"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <Button x:Name="ExcelFilterButton" Tag="{Binding}" Click="ExcelFilterButton_Click" Margin="0,0,0,0" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Focusable="False" Grid.Column="0">
                                    <Image Source="Resources\NoSortNoFilter.png" Width="19" Height="19" />
                                </Button>
                                <TextBlock Text="{Binding}" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="1" />
                            </Grid>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </DataGrid.Resources>
    </DataGrid>

And it comes out nicely . 结果很好

I tried using VisualTreeHelper to find the image from the Column Header, but the Header property is a string. 我尝试使用VisualTreeHelper从列标题中查找图像,但是Header属性是一个字符串。 I've tried using the HeaderStyle and HeaderTemplate properties also but to no avail. 我也尝试过使用HeaderStyle和HeaderTemplate属性,但无济于事。

Using a WPF Spy program called Snoop, I can see the image in there, but still can't figure out how to access it in code. 使用一个名为Snoop的WPF Spy程序,我可以在其中看到图像 ,但是仍然不知道如何通过代码访问它。 The reason I need to access it in code is to change the image based on whether that column is sorted and/or filtered. 我需要在代码中访问它的原因是根据该列是否已排序和/或过滤来更改图像。 (Could this be done in XAML?) (这可以在XAML中完成吗?)

Ok, I figured out how to do it. 好的,我想出了办法。 This most likely not the right way to do it, but I found a way that works. 这很可能不是正确的方法,但是我找到了一种可行的方法。

To give you a little about the process. 给您一些有关的过程。

  1. The user clicks a header button. 用户单击标题按钮。 The buttons Tag property is bound to the column header. 按钮的Tag属性绑定到列标题。
  2. The click event handler instantiates the context menu and sets its Tag to equal the button Tag. click事件处理程序将实例化上下文菜单,并将其Tag设置为等于按钮Tag。
  3. The user clicks on a menu item. 用户单击菜单项。
  4. The event handler sends the Context Menu Tag property and image name to the routine that finds the button, and then the image in the button, and changes the image. 事件处理程序将“上下文菜单标签”属性和图像名称发送到先找到按钮的例程,然后再找到按钮中的图像,然后更改图像。

now for the code. 现在获取代码。

The button click event handler: 按钮单击事件处理程序:

    Private Sub ExcelFilterButton_Click(sender As Object, e As RoutedEventArgs)
        With DirectCast(Resources("sortContextMenu"), ContextMenu)
            .Tag = DirectCast(sender, Button).Tag
            .IsOpen = True
        End With
    End Sub

The menu item click event handler 菜单项单击事件处理程序

Private Sub ContextMenuItem_Click(Sender As Object, e As RoutedEventArgs)
    If TypeOf Sender Is MenuItem Then
        'just testing, of course this isn't all this handler does.
        SetColumnSortImage(Sender.Tag, "Filtered")
    End If
End Sub

The SetColumnSortImage routine, which calls the two following routines. SetColumnSortImage例程,该例程调用以下两个例程。

Private Sub SetColumnSortImage(Tag As String, ImageName As String)
    Dim btn As Button = Nothing
    GetSortButton(Of Button)(dataGrid, Tag, btn)
    If btn IsNot Nothing Then
        Dim img As Image = GetChildOfType(Of Image)(btn)
        img.Source = New BitmapImage(New Uri("pack://application:,,,/Resources/" & ImageName & ".png"))
    End If
End Sub

The GetSortButton routine GetSortButton例程

Private Sub GetSortButton(Of T As DependencyObject)(dep As DependencyObject, Tag As String, ByRef out As DependencyObject)
    If dep IsNot Nothing Then
        If TypeOf dep Is Button AndAlso CType(dep, Button).Tag = Tag Then
            out = dep
        Else
            If VisualTreeHelper.GetChildrenCount(dep) > 0 Then
                For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(dep) - 1
                    GetSortButton(Of T)(VisualTreeHelper.GetChild(dep, i), Tag, out)
                Next
            End If
        End If
    End If
End Sub

This routine was found elsewhere on StackOverflow in C#. 该例程在C#中StackOverflow的其他位置找到。 I converted it to VB. 我将其转换为VB。

Private Function GetChildOfType(Of T As DependencyObject)(depObj As DependencyObject) As T
    If depObj Is Nothing Then
        Return Nothing
    End If

    For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(depObj) - 1
        Dim child = VisualTreeHelper.GetChild(depObj, i)

        Dim result = If(TryCast(child, T), GetChildOfType(Of T)(child))
        If result IsNot Nothing Then
            Return result
        End If
    Next
    Return Nothing
End Function

You may have a better way. 您可能有更好的方法。 Please post if you do. 如果您这样做,请发表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以编程方式在 wpf datagrid 列中显示图像? - How do I show image in wpf datagrid column programmatically? WPF如何将图像源从一种形式更改为另一种形式 - WPF How do i change an image source from a form to another 如何在WPF中动态更改滚动条背景图像? - How do I dynamically change a scrollbar background image in WPF? 如何将 hover 上的图像更改为 WPF 上的图像? - How do I change an image on hover over in WPF? 当选择单元格时,如何突出显示DataGrid的ColumnHeader和RowHeader? - How can I highlight DataGrid's ColumnHeader and RowHeader, when a cell is Selected? WPF DataGrid:如何使DataGridTemplateColumn中的所有按钮具有相同的图像? - WPF DataGrid: How do I get all buttons in a DataGridTemplateColumn to have the same image? 如何在 WPF 运行时更改 Listview 中的图像? - How can I change Image in Listview during runtime in WPF? 如何在运行时在文本之间将图像插入到WPF RichTextBox中,以便文本浮动图像 - How can I insert an image into a WPF RichTextBox at runtime in between text so the text floats around the image 如何在设置器中更改WPF按钮图像 - How can I change WPF Button Image in Setter WPF DataGrid:如何以编程方式更改所选行? - WPF DataGrid: How do I change the selected row programatically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM