0

我在 windows phone xaml 页面中有网格视图,这个网格包含许多 UI 元素,如按钮、复选框和文本框。我想从该网格中按名称搜索一些特定的 UIElement,并希望获取该 UIElement 的值并设置一些新值。我如何在后面的代码中使用 Visual Tree 助手从该网格中获取该 UIElement。

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Grid.RowDefinitions>
                <RowDefinition Height="120" />
                <RowDefinition Height="120" />
                <RowDefinition Height="120" />
                <RowDefinition Height="120" />
                <RowDefinition Height="120" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="240" />
                <ColumnDefinition Width="240" />
            </Grid.ColumnDefinitions>
            <Border BorderThickness="2" HorizontalAlignment="Left" Grid.Row="0" Grid.Column="0" Name="Border1">
                <Image Width="110" Height="110" Stretch="Fill" Name="Image1" Tap="Image1_Tap" />
            </Border>
            <Border BorderThickness="2" BorderBrush="Transparent" HorizontalAlignment="Left" Grid.Row="1" Grid.Column="0" Name="Border2">
                <Image Width="110" Height="110" Stretch="Fill" Name="Image2" Tap="Image2_Tap" />
            </Border>
            <Border BorderThickness="2" HorizontalAlignment="Left" Grid.Row="2" Grid.Column="0" Name="Border3">
                <Image Width="110" Height="110" Stretch="Fill" Name="Image3" Tap="Image3_Tap" />
            </Border>
            <Border BorderThickness="2" HorizontalAlignment="Left" Grid.Row="3" Grid.Column="0" Name="Border4">
                <Image Width="110" Height="110" Stretch="Fill" Name="Image4" Tap="Image4_Tap" />
            </Border>
            <Border BorderThickness="2" HorizontalAlignment="Left" Grid.Row="5" Grid.Column="0" Name="Border5">
                <Image Width="110" Height="110" Stretch="Fill" Name="Image5" Tap="Image5_Tap" />
            </Border>
            <Border BorderThickness="2" HorizontalAlignment="Right" Grid.Row="0" Grid.Column="1" Name="Border11">
                <Image Width="110" Height="110" Stretch="Fill" Name="Image11" Tap="Image11_Tap" />
            </Border>
            <Border BorderThickness="2" HorizontalAlignment="Right" Grid.Row="1" Grid.Column="1" Name="Border22">
                <Image Width="110" Height="110" Stretch="Fill" Name="Image22" Tap="Image22_Tap" />
            </Border>
            <Border BorderThickness="2" HorizontalAlignment="Right" Grid.Row="2" Grid.Column="1" Name="Border33">
                <Image Width="110" Height="110" Stretch="Fill" Name="Image33" Tap="Image33_Tap" />
            </Border>
            <Border BorderThickness="2" HorizontalAlignment="Right" Grid.Row="3" Grid.Column="1" Name="Border44">
                <Image Width="110" Height="110" Stretch="Fill" Name="Image44" Tap="Image44_Tap" />
            </Border>
            <Border BorderThickness="2" HorizontalAlignment="Right" Grid.Row="5" Grid.Column="1" Name="Border55">
                <Image Width="110" Height="110" Stretch="Fill" Name="Image55" Tap="Image55_Tap" />
            </Border>
        </Grid>

我想匹配选项。让我们说左侧一些图像被点击并且它的边框改变并且一旦在右侧点击哪个选项匹配左侧有一些选择或没有

4

1 回答 1

1

你能告诉我你想从网格元素中得到什么吗,因为里面只有边框和图像。您可以通过此通用编码找到元素。(ContentPanel 是您的网格名称),您可以在以下过程中获取 Border 和 Image 等控件名称和其他属性。

 foreach (var child in ContentPanel.Children)
        {
            if (child is Border)
            {
                var borderName = (child as Border).Name;

                // if you wana get the image inside border, then do this.
                var getBorderChlid = (item as Border).Child;
                if (getBorderChlid is Image)
                {
                    var getImgName = (getBorderChlid as Image).Name;
                }

            }
        }

如果这对您有帮助,那么很好,否则请多解释一下您的任务,然后我将向您发送确切的代码 InshaAllah。

于 2015-10-25T13:23:37.253 回答