0

ビジュアルツリーで要素を見つける必要があります。たとえば、グリッドがあり、ExpanderView が展開するときに 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));
        }
4

1 に答える 1

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

            }
        }
于 2012-07-11T22:07:57.993 に答える