[英]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. 给您一些有关的过程。
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.