-1

Windows-phoneテンプレートからItemsPanelを読み取る際に、次のコードを使用します。しかし、それは私にとっては機能していません。ChildrenCountに対して常に0を返します。

    private void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
    {
        StackPanel stackpanel = TraverseVisualTree<StackPanel>(this);
    }

     private T TraverseVisualTree<T>(DependencyObject visual) where T : DependencyObject
     {
        var itemscount = VisualTreeHelper.GetChildrenCount(visual);

        for (int i = 0; i < itemscount; i++)
        {
            var child = VisualTreeHelper.GetChild(visual, i);
            if (child is T)
                return (T)child;
        }
        return null;
      }

誰か助けてくれませんか?

4

1 に答える 1

-1

以下のようにしてみてください、

CustomControl:

<UserControl x:Class='SilverlightApplication3.MainPage'
    xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' 
    xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
    xmlns:d='http://schemas.microsoft.com/expression/blend/2008' 
    xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006' 
    mc:Ignorable='d' 
    d:DesignWidth='640' 
    d:DesignHeight='480'>

        <StackPanel x:Name="LayoutRoot" Background="White">

            <Button x:Name="btnWalkTree" 
                Content="Walk Tree" 
                Click="btnWalkTree_Click"/>

            <Border CornerRadius="10" Background="Yellow">
                <TextBlock x:Name="tbName1"
                   Text="Santa Clause" 
                   HorizontalAlignment="Center"
                   VerticalAlignment="Center" 
                   Margin="3" />
            </Border>

            <Border CornerRadius="10" Background="AliceBlue">
                <TextBlock x:Name="tbName2"
                       Text="Mickey Mouse"
                       HorizontalAlignment="Center"
                       VerticalAlignment="Center"
                       Margin="3" />
            </Border>

            <Border CornerRadius="10" Background="Green">
                <TextBlock x:Name="tbName3"
                       Text="The Tooth Fairy"
                       HorizontalAlignment="Center"
                       VerticalAlignment="Center"
                       Margin="3" />
            </Border>

        </StackPanel>

</UserControl>

カスタム コントロールから子を取得するためのコード:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightApplication3
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void btnWalkTree_Click(object sender, RoutedEventArgs e)
        {
            this.WalkChildren(this);
        }

        private void WalkChildren(DependencyObject depObject)
        {
            string name = String.Empty;
            FrameworkElement element = depObject as FrameworkElement;
            if (element != null)
            {
                name = element.Name;
            }

            int childCount = VisualTreeHelper.GetChildrenCount(element);
            if (childCount > 0)
            {
                for (int i = 0; i < childCount; i++)
                {
                    this.WalkChildren(VisualTreeHelper.GetChild(element, i));
                }
            }
        }
    }
}

VisualTreeHelperから参照

于 2012-07-04T04:45:58.780 に答える