简体   繁体   English

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

[英]How to find element in visual tree in wp7?

I need to find element in visual tree. 我需要在视觉树中找到元素。 For example I have a grid and I need to set my own text in tbox:WatermarkTextBox when ExpanderView expands. 例如,我有一个网格,当ExpanderView展开时,我需要在tbox:WatermarkTextBox中设置自己的文本。 xaml a

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

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

Function that recursively searching through visual tree and set value to the element that you need: 递归搜索视觉树并将值设置为所需元素的功能:

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

            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM