1

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.

Community
  • 1
  • 1
z3nth10n
  • 2,341
  • 2
  • 25
  • 49

1 Answers1

2

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
Dmitry
  • 2,033
  • 1
  • 22
  • 31
  • I don't know why, but I don't find the part that contain the content that I want: http://i.imgur.com/abNdbca.png – z3nth10n Apr 22 '14 at 13:45
  • Point it with the mouse and then press Ctrl+Shift. – Dmitry Apr 22 '14 at 16:38
  • Oh lol, in the tutorial (https://www.youtube.com/watch?v=n8EdRR0Tc1k) I understanded only Control (because I'm spanish :P) – z3nth10n Apr 22 '14 at 16:46
  • And well, the final results is that no source is charged... As you can see there http://gyazo.com/8d35a5d14669559f009215f929238b9e So, now what can I do? D: (I'm not able to continue coding this (I think, because I'm new on WPF)) – z3nth10n Apr 22 '14 at 16:54
  • Perhaps you have another Image with the same name somewhere? – Dmitry Apr 22 '14 at 17:02
  • Do you mean iconKB name, in that case, I have not image called with this name... With the path of the image is the same, this is all the images I try to load: http://pastebin.com/zrsyzcWC – z3nth10n Apr 22 '14 at 17:06
  • Great! There is the project: https://dl.dropboxusercontent.com/s/rfcinlsv93ub2k2/GameLauncher%20WPF.zip?dl=1 The part that we are talking about is in line 169 in MainWindow.xaml.vb and the external clases in Class1.vb :P – z3nth10n Apr 22 '14 at 17:26
  • I think that we are doing this conversation too long, if you can and you want you can add me to Skype, my nick is ikillnukes ;) – z3nth10n Apr 22 '14 at 18:47
  • I found. Just a question do you count to update images overtime? It would be easier to set images in XAML. – Dmitry Apr 22 '14 at 18:57
  • Yes... But, well, I prefer to set it dynamically, because in this way I'm sure that images will be loaded from Resources, and not from another places that can cause an exception. – z3nth10n Apr 22 '14 at 19:12
  • I changed the FW version to 4... Because, it didn't work to a friend, but know it throws in the console the next Exception: First exception of type 'System.NullReferenceException' in GameLauncher WPF.exe (I translated it with Google Translator because the exception is in Spanish) – z3nth10n Apr 25 '14 at 14:49
  • Remove TreeHelper.FindVisualChildByName calls from Load() method. It's actually strange why in 4.5 version this is not causing an exception. It's probably a new feature for VB. I can't really explane it as I never use VB. – Dmitry Apr 25 '14 at 16:03
  • I realised about a thing and is that tabSettings is handled on form Loading... :/ So, I will create another question... – z3nth10n Apr 25 '14 at 18:13
  • And the question is: http://stackoverflow.com/questions/23300873/image-inside-a-collapsed-tab-is-hooked-before-its-container-grid-load-causing – z3nth10n Apr 25 '14 at 18:56