访问DataTemplate内的元素(UWP,C#,Syncfusion TileView)

时间:2018-02-14 16:30:38

标签: c# uwp syncfusion

我试图从后面的代码访问DataTemplate中的控件。我使用的是C#和UWP(通用Windows平台)。 DataTemplate用于Syncfusion控件(TileView)。我的XAML代码是:

                <layout:SfTileView Grid.Row="1" x:Name="tileView" Margin="12, 12, 12, 12" MinimizedItemsOrientation="Left" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding TileViewItems}" SelectedIndex="0" ItemWidth="250" ItemHeight="160">

                    <layout:SfTileView.MaximizedItemTemplate>

                        <DataTemplate>
                            <Border Background="#122544">
                                <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" Background="{Binding HeaderColor}" Tapped="Restore">
                                        <TextBlock Text="{Binding VideoTitle}" FontSize="25" Margin="20" Foreground="White"/>
                                    </Grid>

                                    <Grid Width="100" Height="100" Background="White" Grid.Row="1" Margin="0 20 0 0" Grid.Column="0">
                                        <Image Source="{Binding VideoThumbnail}" Stretch="UniformToFill" Width="100" Height="100"/>
                                    </Grid>

                                    <StackPanel x:Name="DetailsStack" Orientation="Horizontal" Margin="0 20 0 0" Grid.Row="1" Grid.Column="1">

                                        <Grid VerticalAlignment="Top">
                                            <Grid.Resources>
                                                <Style TargetType="TextBlock">
                                                    <Setter Property="FontSize" Value="14"/>
                                                    <Setter Property="Foreground" Value="White"/>
                                                </Style>
                                            </Grid.Resources>

                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="25"/>
                                                <RowDefinition Height="25"/>
                                                <RowDefinition Height="25"/>
                                                <RowDefinition Height="25"/>
                                            </Grid.RowDefinitions>

                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="150"/>
                                                <ColumnDefinition/>
                                            </Grid.ColumnDefinitions>

                                            <TextBlock Text="Title "/>
                                            <TextBlock Text="{Binding VideoTitle}" Grid.Column="1"/>
                                            <TextBlock Text="Uploaded By " Grid.Row="1"/>
                                            <TextBlock Text="{Binding VideoCreator}" Grid.Row="1" Grid.Column="1"/>
                                            <TextBlock Text="Video ID " Grid.Row="2"/>
                                            <TextBlock x:FieldModifier="Public" x:Name="IdString" Text="{Binding VideoId}" Grid.Row="2" Grid.Column="1"/>
                                            <TextBlock Text="Views " Grid.Row="3"/>
                                            <TextBlock Text="{Binding VideoViews}" Grid.Row="3" Grid.Column="1"/>

                                        </Grid>

                                    </StackPanel>

                                    <TextBlock Text="{Binding VideoDescription}" MaxLines="10" Grid.ColumnSpan="2" Grid.Row="2" VerticalAlignment="Top" TextWrapping="Wrap" Margin="20" FontSize="14" FontWeight="Light" Foreground="White" />

                                    <StackPanel VerticalAlignment="Center" Margin="20 0" Orientation="Horizontal" Grid.Row="3" Grid.RowSpan="4" 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/>
                                                <ColumnDefinition/>
                                                <ColumnDefinition/>
                                                <ColumnDefinition/>
                                                <ColumnDefinition/>
                                            </Grid.ColumnDefinitions>
                                            <FontIcon FontFamily="Segoe MDL2 Assets" Glyph="&#xE890;" Margin="5 0"/>
                                            <TextBlock Text="{Binding VideoViews}" Grid.Column="1" Margin="5 0" VerticalAlignment="Center"/>
                                            <FontIcon FontFamily="Segoe MDL2 Assets" Glyph="&#xE8E1;" Grid.Column="2" Margin="5 0" />
                                            <TextBlock Text="{Binding VideoLikes}" Grid.Column="3" Margin="5 0" VerticalAlignment="Center"/>
                                            <FontIcon FontFamily="Segoe MDL2 Assets" Glyph="&#xE8E0;" Grid.Column="4" Margin="5 0" />
                                            <TextBlock Text="{Binding VideoDislikes}" Grid.Column="5" Margin="5 0" VerticalAlignment="Center"/>
                                            <FontIcon FontFamily="Segoe MDL2 Assets" Glyph="&#xE90A;" Grid.Column="6" Margin="5 0" />
                                            <TextBlock Text="{Binding VideoComments}" Grid.Column="7" Margin="5 0" VerticalAlignment="Center"/>
                                        </Grid>
                                        <StackPanel x:Name="ButtonsStack" Orientation="Horizontal">
                                            <TextBlock x:Name="HiddenID" Text="{Binding VideoId}" Visibility="Collapsed" />
                                            <Button x:Name="WatchVideo" Content="Watch Video" Tapped="WatchVideo_Tapped" />
                                            <Button x:Name="DownloadVideo" Content="Download MP4" />
                                        </StackPanel>
                                    </StackPanel>

                                </Grid>
                            </Border>
                        </DataTemplate>
                    </layout:SfTileView.MaximizedItemTemplate>
                    <layout:SfTileView.ItemTemplate>
                        <DataTemplate>
                            <Grid>
                                <Notification:SfHubTile Width="250" Height="160" Foreground="White" ScaleDepth="0.9" RotationDepth="10" Padding="2" Background="{Binding TileColor}">

                                    <Notification:SfHubTile.Content>
                                        <Grid>
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="*"/>
                                                <RowDefinition Height="*"/>
                                            </Grid.RowDefinitions>
                                            <Image Source="{Binding VideoThumbnail}" Stretch="UniformToFill" Width="250"/>
                                            <Grid Background="{Binding HeaderColor}" Grid.Row="1">
                                                <TextBlock Text="{Binding VideoTitle}" FontSize="14" Margin="8" TextWrapping="Wrap" HorizontalAlignment="Stretch"/>
                                            </Grid>
                                        </Grid>
                                    </Notification:SfHubTile.Content>

                                </Notification:SfHubTile>
                            </Grid>
                        </DataTemplate>
                    </layout:SfTileView.ItemTemplate>
                </layout:SfTileView>

我需要访问的控件是一个隐藏的TextBlock,名为&#34; HiddenID&#34; (位于StackPanel内部,名为&#34; ButtonsStack&#34;)。

有没有人知道如何访问DataTemplate中的控件?我已经尝试了我能想到的一切:)

提前致谢。

2 个答案:

答案 0 :(得分:2)

通常,可以使用VisualTreeHelper检索DataTemplate中的元素。我创建了一个示例并上传了here如果有帮助,请告诉我。

注意:我为Syncfusion工作。

答案 1 :(得分:0)

可以在另一个问题上找到here这个问题的答案。您无法访问控件的原因是您创建了此数据模板的多个实例,其中控件的名称会相互冲突。

要解决此问题,您必须使用获取所有子项的方法,然后搜索您尝试访问的控件。完整的解决方案可以在上面发布的链接中找到。