在TabItem中访问DataTemplate内部的元素 [英] Accessing elements inside a DataTemplate in a TabItem

查看:158
本文介绍了在TabItem中访问DataTemplate内部的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

,我在

[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

XAML部分:

<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>

因此,我尝试使用以下语法编辑iconKB图像的源代码:

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))

但是由于某些原因,它没有改变。它保持空白。 (问题不在 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)

谢谢。

推荐答案

这是因为它仅在DataTemplate的名称范围内定义。想想看,当您运行应用程序时,可能会有很多,而所有这些都不能称为iconKB。

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.

编辑:好的,我检查了您的代码。没关系。导致其无法正常运行的原因是,由于未打开选项卡,因此您尝试查找VisualTree中尚未存在的元素。因此找不到该图像。
如果您在Loaded事件处理程序中编写它,它将起作用。

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

这篇关于在TabItem中访问DataTemplate内部的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆