XAML 正在从列表视图项模板获取UI元素

6za6bjd0  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(145)

我在我的应用程序中有ListView,它有风格。

<ListView x:Name="FoldersListView"
          RightTapped="OnFoldersListViewRightTapped"                        
          Style="{StaticResource FoldersListViewStyle}"
          ItemsSource="{Binding Source={StaticResource Locator}, Path=MainViewModel.Folders}"
          SelectedItem="{Binding Source={StaticResource Locator}, Path=MainViewModel.SelectedFolder, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/>

在xaml live树中,我在ListView中加载了网格(ItemTemplate的DataTemplates)

如何在代码隐藏中从ListView获取每个Grid?

foreach (var item in this.FoldersListView.Items)
{
    var recievedItem = this.FoldersListView.ContainerFromItem(item) as ListViewItem;
}
rhfm7lfc

rhfm7lfc1#

如何在代码隐藏中从ListView获取每个Grid?
您将需要使用VisualTreeHelper Class来实现这一点。
1.通过调用ListView的ContainerFromItemContainerFromIndex获取每个ListViewItem
1.创建一个递归函数来查找每个ListViewItem中网格化DependencyObject。
在不了解你的真实的场景的情况下,我只是做了一个简单的演示来展示它是如何工作的。你可以参考下面的代码,并根据你的场景稍微修改一下。

主页面.Xaml

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Button Content="Click" Click="Button_Click"/>
    <ListView Grid.Row="1" x:Name="MyListView">
        <ListView.ItemTemplate>
            <DataTemplate x:DataType="x:String">
                <Grid >
                    <TextBlock Text="{x:Bind}"/>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

主页.Cs

public List<string> sourceList { get; set; }
    public MainPage()
    {
        this.InitializeComponent();
        sourceList = new List<string>();
        sourceList.Add("1");
        sourceList.Add("2");
        sourceList.Add("3");
        sourceList.Add("4");
        sourceList.Add("5");
        MyListView.ItemsSource = sourceList;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        foreach (var stringItem in sourceList) 
        {
            // get every listview item first
            ListViewItem item  = MyListView.ContainerFromItem(stringItem) as ListViewItem;
            // the DependencyObject is the Grid that you want to get
            DependencyObject grid = FindChild(item);
        }
    }

    // a recursive function to find DependencyObjects that are grid
    public DependencyObject FindChild(DependencyObject parant) 
    {
        int count = VisualTreeHelper.GetChildrenCount(parant);

        for (int i = 0; i < count; i++)
        {
            var MyChild = VisualTreeHelper.GetChild(parant, i);
            if (MyChild is Grid)
            {
                // My datatemplate is simple, so I could just get the TextBlock. 
                //If you are using a different datatemplate, you will need to modify the following code to do want you want.
                Grid myGrid = (Grid)MyChild;
                TextBlock textBlock = (TextBlock)myGrid.Children.FirstOrDefault();

                textBlock.Foreground = new SolidColorBrush(Colors.Red);

                return myGrid;
            }
            else
            {
                var FindResult = FindChild(MyChild);
                return FindResult;
            }
        }
        return null;
    }

相关问题