Accessing elements inside a DataTemplate in a TabItem

我怕爱的太早我们不能终老 提交于 2019-12-24 07:09:16

问题


Well, I have a problem with a function that I get from there

[VB.NET]

Public Class TreeHelper

Public Shared Function FindVisualChildByName(Of T As FrameworkElement)(parent As DependencyObject, name As String) As T
    Dim child As T = Nothing
    For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(parent) - 1
        Dim ch = VisualTreeHelper.GetChild(parent, i)
        child = TryCast(ch, T)
        If child IsNot Nothing AndAlso child.Name = name Then
            Exit For
        Else
            child = FindVisualChildByName(Of T)(ch, name)
        End If

        If child IsNot Nothing Then
            Exit For
        End If
    Next
    Return child
End Function

End Class

And the XAML part:

<TabItem x:Name="itemControls" 
     Height="50"
     Margin="0"
     VerticalAlignment="Top"
     HorizontalContentAlignment="Stretch"
     VerticalContentAlignment="Stretch"
     Padding="6,1">
<TabItem.HeaderTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal">
            <Image x:Name="iconKB"
                   Width="25"
                   Height="25"
                   Stretch="Fill" />
        </StackPanel>
    </DataTemplate>
</TabItem.HeaderTemplate>
</TabItem>

So, I tried to edit iconKB image's source with the following syntax:

TreeHelper.FindVisualChildByName(Of Image)(itemControls, "iconKB").Source = New BitmapImage(New Uri("pack://application:,,,/Resources/icons/Keyboard.png"))

But for some reason it doesn't change. It keeps blank. (And the problem is not in New BitmapImage(New Uri("pack://application:,,,/Resources/icons/Keyboard.png")) it's completely checked with another image controls)

Thanks in advance.


回答1:


It's because it is defined only inside namescope of the DataTemplate. Think about it, when you run your application you could have plenty of them and all of them can't be called iconKB.

EDIT: Ok i checked your code. It's ok. The thing that makes it don't behave correctly is that you try to find an element that is not yet in the VisualTree because the tab is not opened. So the image is not found. If you write it in Loaded event handler it will work.

Private Shadows Sub TSLoaded() Handles tabSettings.Loaded
    TreeHelper.FindVisualChildByName(Of Image)(itemControls, "iconKB").Source = New BitmapImage(New Uri("pack://application:,,,/Resources/icons/Keyboard.png"))
    TreeHelper.FindVisualChildByName(Of Image)(itemMouse, "iconMouse").Source = New BitmapImage(New Uri("pack://application:,,,/Resources/icons/Mouse.png"))
    TreeHelper.FindVisualChildByName(Of Image)(itemAudio, "iconAudio").Source = New BitmapImage(New Uri("pack://application:,,,/Resources/icons/Audio.png"))
    TreeHelper.FindVisualChildByName(Of Image)(itemVideo, "iconVideo").Source = New BitmapImage(New Uri("pack://application:,,,/Resources/icons/Video.png"))
    TreeHelper.FindVisualChildByName(Of Image)(itemSettings, "iconSettings").Source = New BitmapImage(New Uri("pack://application:,,,/Resources/icons/Settings.png"))
End Sub


来源:https://stackoverflow.com/questions/23198626/accessing-elements-inside-a-datatemplate-in-a-tabitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!