如何在wp7中找到可视树中的元素?

时间:2012-07-11 22:07:57

标签: c# windows-phone-7 visual-tree

我需要在可视树中找到元素。例如,我有一个网格,我需要在扩展时在tbox:WatermarkTextBox中设置我自己的文本。 XAML

<Grid.ColumnDefinitions>
                                <ColumnDefinition Width="100"/>
                                <ColumnDefinition Width="280"/>
                                <ColumnDefinition Width="100"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition/>
                                <RowDefinition/>
                            </Grid.RowDefinitions>
                            <Grid Grid.Row="0" Grid.Column="0" >
                                <Border/>
                            </Grid>
                            <Grid Grid.Row="0" />
                            <Grid Grid.Row="0" Grid.Column="2"  />
                            <toolkit:ExpanderView Expanded="setText" Collapsed="hideAppBar">
                                <Image Height="100" Margin="-53,0,0,0"/>
                            </toolkit:ExpanderView>
                            <toolkit:ExpanderView x:Name="expText"  IsExpanded="False" Tag="{Binding}" Grid.Row="1" Grid.Column="1" VerticalContentAlignment="Stretch" Grid.ColumnSpan="2" Background="White" Foreground="Black" BorderBrush="White">
                                <tbox:WatermarkTextBox TextChanged="DisableOrEnable" TextWrapping="Wrap" AcceptsReturn="True" WatermarkText="Введите комментарий"  BorderThickness="0" InputScope="Chat" Margin="-51,0,0,0" Padding="0" Background="White" BorderBrush="White"/>
                            </toolkit:ExpanderView>
                           ...many elements
                        </Grid>

C#

public string message="my message";
private void setText(object sender, RoutedEventArgs e)
        {
            setMessage(((sender as ExpanderView).Parent as Grid));
        }

1 个答案:

答案 0 :(得分:1)

递归搜索可视树并将值设置为您需要的元素的函数:

public void setMessage(DependencyObject parent)
        {
            if (parent == null)
            {
                return;
            }


            int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < childrenCount; i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(parent, i);
                var frameworkElement = child as FrameworkElement;

                if (frameworkElement != null && frameworkElement is WatermarkTextBox/*Type of element that you need*/)
                {
                    (frameworkElement as WatermarkTextBox).Text = message;/*Value that you need*/
                    break;

                }else
                    if (frameworkElement != null)
                    {
                        int CountInChildren = VisualTreeHelper.GetChildrenCount(frameworkElement);
                        for (int z = 0; z < CountInChildren; z++)
                        {
                            DependencyObject child1 = VisualTreeHelper.GetChild(frameworkElement, z);
                            setMessage(frameworkElement);
                        }
                    }

            }
        }