UWP - использовать шаблон данных внутри стиля в GridVIew - PullRequest
0 голосов
/ 28 сентября 2018

Ошибка при попытке нажать на элемент в сетке.У меня есть такой установщик стиля:

<Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="GridViewItem">
                <ListViewItemPresenter x:Name="Root" ContentTemplate="{StaticResource CompletedDataTemplate}">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Pressed">                                        
                               <Storyboard>
                                    <DoubleAnimation Storyboard.TargetName="Item" 
                                                     Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.ScaleX)" To="1.15" BeginTime="00:00:00.2000000"/>

                                <DoubleAnimation Duration="00:00:00.4" Storyboard.TargetName="Item" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.ScaleY)" To="1.15" BeginTime="00:00:00.2000000"/>
                                <DoubleAnimationUsingKeyFrames EnableDependentAnimation="True" Storyboard.TargetName="grid" Storyboard.TargetProperty="(FrameworkElement.Width)">
                                        <EasingDoubleKeyFrame KeyTime="00:00:00.5" Value="471"/>
                                    </DoubleAnimationUsingKeyFrames>                                   
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                        <VisualStateGroup x:Name="DisabledStates">
                            <VisualState x:Name="Enabled"/>
                            <VisualState x:Name="Disabled"/>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                </ListViewItemPresenter>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

А также у меня есть такой шаблон данных:

<DataTemplate x:Key="CompletedDataTemplate">
    <Grid x:Name="Item" Width="471" Height="303">

        <Grid Height="265" VerticalAlignment="Top">
            <Image Stretch="Fill" Source="../Assets/Fantastic-Full-HD-Wallpaper.jpg"/>
        </Grid>

        <TextBlock x:Name="textBlock" Height="20" TextAlignment="Center" Text="{Binding Name}" FontSize="21" FontFamily="../Assets/Fonts/GothamPro.ttf#Gotham Pro" Foreground="White" TextLineBounds="TrimToCapHeight"
                   VerticalAlignment="Bottom"/>

        <Grid x:Name="grid" HorizontalAlignment="Left" Height="267" VerticalAlignment="Top" Width="0" Opacity="20">
            <Grid.Background>
                <AcrylicBrush TintColor="Black" TintOpacity="200"/>
            </Grid.Background>
            <TextBlock FontSize="64" Text="Open" Foreground="White" TextAlignment="Center" VerticalAlignment="Center"></TextBlock>
        </Grid>
    </Grid>
</DataTemplate>

И когда я запускаю свое приложение, шаблоны и привязка работают отлично, но я не знаюкак установить целевое имя раскадровки для моих элементов шаблона данных.Любая идея?Может быть, я должен использовать другое решение?

В общем, у меня такая проблема: мне нужно использовать разные стили в моем виде сетки и разные шаблоны данных.Я уже пытался использовать визуальные состояния в моем шаблоне данных, но, похоже, это не сработало.

1 Ответ

0 голосов
/ 01 октября 2018

Мне нужно использовать разные стили в сетке и разные шаблоны данных.

Невозможно разрешить это имя, поскольку это имя является локальным для шаблона данных.Вы можете переместить раскадровку в шаблоне данных и запустить ее при нажатии сетки.

Для достижения этого эффекта необходимо использовать действие ControlStoryboardAction в Поведение XAML .

См. Следующий пример кода для ссылки:

<DataTemplate x:Key="CompletedDataTemplate">
        <Grid x:Name="Item" Width="471" Height="303">
            <Grid.Resources>
                <Storyboard x:Key="storyboard">
                    <DoubleAnimation Storyboard.TargetName="Item" 
                                                 Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.ScaleX)" To="1.15" BeginTime="00:00:00.2000000"/>

                    <DoubleAnimation Duration="00:00:00.4" Storyboard.TargetName="Item" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.ScaleY)" To="1.15" BeginTime="00:00:00.2000000"/>
                    <DoubleAnimationUsingKeyFrames EnableDependentAnimation="True" Storyboard.TargetName="grid" Storyboard.TargetProperty="(FrameworkElement.Width)">
                        <EasingDoubleKeyFrame KeyTime="00:00:00.5" Value="471"/>
                    </DoubleAnimationUsingKeyFrames>
                </Storyboard>
            </Grid.Resources>
            <Interactivity:Interaction.Behaviors>
                <Interactions:EventTriggerBehavior EventName="Tapped">
                    <Media:ControlStoryboardAction Storyboard="{StaticResource storyboard}"/>
                </Interactions:EventTriggerBehavior>
            </Interactivity:Interaction.Behaviors>
            <Grid Height="265" VerticalAlignment="Top">
                <Image Stretch="Fill" Source="../Assets/Fantastic-Full-HD-Wallpaper.jpg"/>
            </Grid>

            <TextBlock x:Name="textBlock" Height="20" TextAlignment="Center" Text="{Binding Name}" FontSize="21" TextLineBounds="TrimToCapHeight"
               VerticalAlignment="Bottom"/>

            <Grid x:Name="grid" HorizontalAlignment="Left" Height="267" VerticalAlignment="Top" Width="0" Opacity="20">
                <Grid.Background>
                    <AcrylicBrush TintColor="Black" TintOpacity="200"/>
                </Grid.Background>
                <TextBlock FontSize="64" Text="Open" Foreground="White" TextAlignment="Center" VerticalAlignment="Center"></TextBlock>
            </Grid>
        </Grid>
</DataTemplate>

Затем, если вы хотите применить другой DataTemplate для элемента GridView, вы можете использовать DataTemplateSelector .

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...