簡體   English   中英

如何在wp7中的可視樹中查找元素?

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

我需要在視覺樹中找到元素。 例如,我有一個網格,當ExpanderView展開時,我需要在tbox:WatermarkTextBox中設置自己的文本。 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#

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

遞歸搜索視覺樹並將值設置為所需元素的功能:

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