Articles in this section
Category / Section

How to access the element inside the maximized item template in SfTileView?

2 mins read

We can retrieve the element in the MaximizedItemTemplate of UWP SfTileView Item using GetChild() method of VisualTreeHelper class. For instance, we have retrieved the border(“MaximizedItemBorder”) element from the MaximizedTileItem template and change the background color of the element. The following code illustrate the same,

Template for MaximizedTileItem:

     <DataTemplate x:Key="MaximizedTileItemTemplate">
            <Border x:Name="MaximizedItemborder">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="150"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Grid  Grid.ColumnSpan="2" >
                        <TextBlock Text="{Binding}" FontSize="25" 
                                        Margin="20" Foreground="Black"/>
                    </Grid>
 
                    <RichTextBlock Grid.ColumnSpan="2" Grid.Row="2" VerticalAlignment="Top"  TextWrapping="Wrap"
                                           Margin="20" FontSize="14" FontWeight="Light" Foreground="Black">
                        <Paragraph LineHeight="30">
                            <Run>
                                 SfTileView acts as a container that holds a set of tile view items that can contain any content. Items can be maximized, minimized, dragged to reorder and arranged in matrix position to achieve best layout.
                            </Run>
                        </Paragraph>
                    </RichTextBlock>
                    <StackPanel VerticalAlignment="Center" Margin="20 0" Orientation="Horizontal" Grid.Row="3" Grid.ColumnSpan="2">
                        <Grid>
                            <Grid.Resources>
                                <Style TargetType="TextBlock">
                                    <Setter Property="FontSize" Value="14"/>
                                    <Setter Property="Foreground" Value="White"/>
                                </Style>
                            </Grid.Resources>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                                <ColumnDefinition/>
                                <ColumnDefinition/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            <Image Source="ms-appx:///Image/Emp_02.png" Margin="5 0" />
                            <TextBlock Text="{Binding Phone}" Grid.Column="1" Margin="5 0" VerticalAlignment="Center"/>
                        </Grid>
                    </StackPanel>
 
                </Grid>
            </Border>
        </DataTemplate>

 

MainPage.xaml:

<syncfusion:SfTileView Grid.Row="1" x:Name="sftileview" MaximizedItemTemplate="{StaticResource MaximizedTileItemTemplate}">
            <syncfusion:SfTileViewItem Content="TileItem1" Background="AliceBlue"/>
            <syncfusion:SfTileViewItem Content="TileItem2" Background="Orange"/>
            <syncfusion:SfTileViewItem Content="TileItem3" Background="Blue"/>
            <syncfusion:SfTileViewItem Content="TileItem4" Background="Coral"/>
            <syncfusion:SfTileViewItem Content="TileItem5" Background="DarkCyan"/>
        </syncfusion:SfTileView>

 

Method to get the required element:

public IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
        {
            if (depObj != null)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
 
                    if (child != null && child is T)
                        yield return (T)child;
 
                    foreach (T childOfChild in FindVisualChildren<T>(child))
                        yield return childOfChild;
                }
            }
        }

 

Method to change the background of the Border element:

private void ChangeBackColor()
        {
            foreach (Border border in FindVisualChildren<Border>(this))
            {
                if (border.Name == "MaximizedItemborder")
                {
                    Random r = new Random();
                    Color randomColor = Color.FromArgb((byte)r.Next(0, 256), (byte)(r.Next(0, 256)), (byte)0, (byte)(r.Next(0, 256)));
                    border.Background = new SolidColorBrush(randomColor);
                }
            }
        }

 

Sample : https://www.syncfusion.com/downloads/support/directtrac/general/ze/TileViewSample_MaximizedItemTemplate-1710628963.zip

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied