none
[UWP] [XAML] Change properties of a single child within a GridView RRS feed

  • Question

  • Hello.  I have a GridView that dynamically adds items based from a List.  I will be finding the index in the list where a the string is equal to a scanned string.  Once I have the index, how would I target the child with that index and change the properties of the RelativePanel within it?

                    <GridView x:Name="myGridView"
                              HorizontalAlignment="Stretch"
                              RelativePanel.Below="buttonOne"
                              Margin="5,4,0,12">
                        <GridView.ItemTemplate>
                            <DataTemplate x:DataType="data:Charge">
                                <Grid Style="{StaticResource '1x1 GridView'}" Holding="Grid_Holding">
                                    <RelativePanel Margin="5,5,5,5" VerticalAlignment="Center">
                                        <TextBlock Text="{x:Bind data1}" Name="NameTxt" Style="{StaticResource DataStyle}" Width="103" Height="Auto" TextAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap"/>
                                        <TextBlock Text="{x:Bind data2}" Name="DescriptionTxt" Style="{StaticResource DataStyle}" TextWrapping="Wrap" Height="Auto" TextAlignment="Center" Width="103" RelativePanel.Below="{Binding ElementName=NameTxt}"/>
                                    </RelativePanel>
                                </Grid>
                            </DataTemplate>
                        </GridView.ItemTemplate>
                    </GridView>
    

    Wednesday, August 31, 2016 5:58 PM

Answers

  • Hi Roman Jones,

    for getting controls from DateTemplate you should use the VishualTree

    public List<FrameworkElement> AllChildren(DependencyObject parent)
            {
                var _List = new List<FrameworkElement>();
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
                {
                    var _Child = VisualTreeHelper.GetChild(parent, i);
                    if (_Child is FrameworkElement)
                        _List.Add(_Child as FrameworkElement);
                    _List.AddRange(AllChildren(_Child));
                }
                return _List;
            }
    
            private void button_Click(object sender, RoutedEventArgs e)
            {
                int index = 5555555;
                var item = gridView.Items[index];
                var _Container = gridView.ContainerFromItem(item);
                var _Children = AllChildren(_Container);
                var _FirstName = _Children.OfType<TextBlock>().First(x => x.Name.Equals("DescriptionTxt"));
                _FirstName?.SetValue(RelativePanel.AlignTopWithPanelProperty, true);
            }


    Make the community better together

    Thursday, September 1, 2016 9:58 AM
  • Hi Roman Jones,

    >>” Once I have the index, how would I target the child with that index”

    You could specify index value to “GridView.SelectedIndex” property.

     myGridView.SelectedIndex = 1;

    >>” change the properties of the RelativePanel”

    You could use my following method to find the RelativePanel.

    <GridView x:Name="myGridView"
                              HorizontalAlignment="Stretch"
                              RelativePanel.Below="buttonOne"
                              Margin="5,4,0,12">
                <GridView.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <RelativePanel x:Name="relativepanel" Margin="5,5,5,5" VerticalAlignment="Center">
                                <TextBlock Text="{Binding data1}" Name="NameTxt"  Width="103" Height="Auto" TextAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap"/>
                                <TextBlock Text="{Binding data2}" Name="DescriptionTxt"  TextWrapping="Wrap" Height="Auto" TextAlignment="Center" Width="103" RelativePanel.Below="{Binding ElementName=NameTxt}"/>
                            </RelativePanel>
                        </Grid>
                    </DataTemplate>
                </GridView.ItemTemplate>
            </GridView>
    private List<FrameworkElement> GetChildren(DependencyObject parent)
            {
                List<FrameworkElement> controls = new List<FrameworkElement>();
    
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); ++i)
                {
                    var child = VisualTreeHelper.GetChild(parent, i);
                    if (child is FrameworkElement)
                    {
                        controls.Add(child as FrameworkElement);
                    }
                    controls.AddRange(GetChildren(child));
                }
    
                return controls;
            }
    var item = myGridView.SelectedItem;
                var container = myGridView.ContainerFromItem(item);
                var control = (RelativePanel)GetChildren(container).First(x => x.Name == "relativepanel");

    Best Regards,

    Xavier Eoro


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, September 1, 2016 11:20 AM
    Moderator

All replies

  • Hi Roman Jones,

    for getting controls from DateTemplate you should use the VishualTree

    public List<FrameworkElement> AllChildren(DependencyObject parent)
            {
                var _List = new List<FrameworkElement>();
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
                {
                    var _Child = VisualTreeHelper.GetChild(parent, i);
                    if (_Child is FrameworkElement)
                        _List.Add(_Child as FrameworkElement);
                    _List.AddRange(AllChildren(_Child));
                }
                return _List;
            }
    
            private void button_Click(object sender, RoutedEventArgs e)
            {
                int index = 5555555;
                var item = gridView.Items[index];
                var _Container = gridView.ContainerFromItem(item);
                var _Children = AllChildren(_Container);
                var _FirstName = _Children.OfType<TextBlock>().First(x => x.Name.Equals("DescriptionTxt"));
                _FirstName?.SetValue(RelativePanel.AlignTopWithPanelProperty, true);
            }


    Make the community better together

    Thursday, September 1, 2016 9:58 AM
  • Hi Roman Jones,

    >>” Once I have the index, how would I target the child with that index”

    You could specify index value to “GridView.SelectedIndex” property.

     myGridView.SelectedIndex = 1;

    >>” change the properties of the RelativePanel”

    You could use my following method to find the RelativePanel.

    <GridView x:Name="myGridView"
                              HorizontalAlignment="Stretch"
                              RelativePanel.Below="buttonOne"
                              Margin="5,4,0,12">
                <GridView.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <RelativePanel x:Name="relativepanel" Margin="5,5,5,5" VerticalAlignment="Center">
                                <TextBlock Text="{Binding data1}" Name="NameTxt"  Width="103" Height="Auto" TextAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap"/>
                                <TextBlock Text="{Binding data2}" Name="DescriptionTxt"  TextWrapping="Wrap" Height="Auto" TextAlignment="Center" Width="103" RelativePanel.Below="{Binding ElementName=NameTxt}"/>
                            </RelativePanel>
                        </Grid>
                    </DataTemplate>
                </GridView.ItemTemplate>
            </GridView>
    private List<FrameworkElement> GetChildren(DependencyObject parent)
            {
                List<FrameworkElement> controls = new List<FrameworkElement>();
    
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); ++i)
                {
                    var child = VisualTreeHelper.GetChild(parent, i);
                    if (child is FrameworkElement)
                    {
                        controls.Add(child as FrameworkElement);
                    }
                    controls.AddRange(GetChildren(child));
                }
    
                return controls;
            }
    var item = myGridView.SelectedItem;
                var container = myGridView.ContainerFromItem(item);
                var control = (RelativePanel)GetChildren(container).First(x => x.Name == "relativepanel");

    Best Regards,

    Xavier Eoro


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, September 1, 2016 11:20 AM
    Moderator