0

我有以下 XAML,但我似乎无法访问相应 .CS 文件中 ItemsWrapGrid 的内容 - 谁能告诉我我应该做什么(这是背​​后的代码和 xaml):

private void wifiTapped(object sender, TappedRoutedEventArgs e)
        {
            Debug.WriteLine("in here " + e.GetType().ToString());
            ItemsWrapGrid wg = (ItemsWrapGrid) sender;

            Debug.WriteLine(e.OriginalSource.ToString());
            foreach (Control c in wg.Children)
            {
                Debug.WriteLine("Control " + c.Name);

            }

            Debug.WriteLine("leaving ");
        }





<GridView VerticalAlignment="Top" ItemsSource="{Binding nets}" x:Name="GDView" ItemClick="gdViewClick" >
            <GridView.ItemTemplate>
                <DataTemplate x:Name="configDataTemplate" x:DataType="data:wifiNets" >
                    <StackPanel Height="300" Width="350" Margin="10" Name="dtStackPanel" >
                        <Image Source="Assets\wifiIcon.png" Width="200" Height="201"  />
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="Name" Margin="0,0,10,0"/>
                            <TextBlock Name="configSSID" Width="auto"  Text="{x:Bind NetSSID}" FontSize="24" />
                        </StackPanel>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="Strength"  Margin="0,0,10,0"/>
                            <!--<TextBlock Name="configStrength" Width="auto" Text="{x:Bind NetSSIDStrength}" FontSize="20" />-->
                            <ProgressBar Name="configProgBar" Maximum="5" Value="{x:Bind NetSSIDStrength}" Foreground="Green"  />
                        </StackPanel>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="Connected" Margin="0,0,10,0"/>
                            <TextBlock Name="configConnectedTo" Text="{x:Bind NetSSIDConnected}" FontSize="20"/>
                        </StackPanel>

                    </StackPanel>
                </DataTemplate>
            </GridView.ItemTemplate>

            <GridView.ItemsPanel>
                <ItemsPanelTemplate>
                    <ItemsWrapGrid MaximumRowsOrColumns="10" Orientation="Vertical"  Tapped="wifiTapped" />
                </ItemsPanelTemplate>
            </GridView.ItemsPanel>
        </GridView> 

为了清楚起见,当我运行它时,我有三项数据(所以它正在工作) - 但我想做的是点击这三个数据项中的任何一项并能够识别各个控件以及它们在数据面板中的值。

在此先感谢,这让我发疯了。保罗。

4

1 回答 1

1

Set IsItemClickEnabled to true on your GridView, and hook into the ItemClick event on the GridView itself. From the event args, you can get the sender (most likely the GridViewItem UI element itself, of which your DataTemplate content is a child), and the ClickedItem, which is the bound datacontext of the datatemplate - in your case the instance of the data:wifiNets - which if your bindings work mean you won't actually have to look in the VisualTree at all.

如果出于某种原因您想通过 VisualChildren 对任何 ItemsControl 的项目进行递归,请使用 ItemsControl 上的ContainerFromIndexorContainerFromItem方法来获取托管数据模板的每个实例的 ItemContainer - 尽管我不建议您这样做,除非您真的需要。理想情况下,您不应该经常需要手动拖网可视化树。

于 2018-02-26T16:53:53.233 回答