使用AdaptiveTrigger的视觉状态不起作用

时间:2016-01-09 22:40:29

标签: c# xaml user-controls uwp visualstatemanager

我在PivotItem中有ListView。我的数据模板是UserControl,它包含几个具有自适应触发器的可视状态。问题是我的视觉状态不起作用。有什么问题?

我的ListView标记的一部分:

<ListView.ItemTemplate>
          <DataTemplate x:DataType="models:RecommendedAnime">
               <controls:RecomendationControl/>
          </DataTemplate>
</ListView.ItemTemplate>

我的UserControl xaml:

<UserControl
x:Class="MyAnimelistRT.Controls.RecomendationControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:system="using:System"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<VisualStateManager.VisualStateGroups>
    <VisualStateGroup x:Name="WindowStates">
        <VisualState x:Name="NarrowState">
            <VisualState.StateTriggers>
                <AdaptiveTrigger MinWindowWidth="0" />
            </VisualState.StateTriggers>
            <VisualState.Setters>
                <Setter Target="ImageCover.(RelativePanel.Below)" Value="TextBlockCover" />
                <Setter Target="ListViewRecomendations.(RelativePanel.Below)" Value="ImageCover" />
            </VisualState.Setters>
        </VisualState>
        <VisualState x:Name="WideState">
            <VisualState.StateTriggers>
                <AdaptiveTrigger MinWindowWidth="500" />
            </VisualState.StateTriggers>
            <VisualState.Setters>
                <Setter Target="TextBlockCover.(RelativePanel.RightOf)" Value="ImageCover" />
                <Setter Target="ListViewRecomendations.(RelativePanel.RightOf)" Value="ImageCover" />
                <Setter Target="ListViewRecomendations.(RelativePanel.Below)" Value="TextBlockCover" />
            </VisualState.Setters>
        </VisualState>
    </VisualStateGroup>
</VisualStateManager.VisualStateGroups>

<RelativePanel>
    <TextBlock x:Name="TextBlockCover" Style="{StaticResource SubheaderTextBlockStyle}" Text="{x:Bind Item.Title,Mode=OneWay}" Margin="4,0,0,0"/>
    <Image x:Name="ImageCover" HorizontalAlignment="Left" Source="{x:Bind Item.ImageUrl,Mode=OneWay}" Stretch="None"/>
    <ListView x:Name="ListViewRecomendations" ItemsSource="{x:Bind Item.Recomendations,Mode=OneWay}" SelectionMode="None" IsItemClickEnabled="False">
        <ListView.ItemTemplate>
            <DataTemplate x:DataType="system:String">
                <TextBlock Style="{StaticResource BodyTextBlockStyle}" Text="{x:Bind}"/>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</RelativePanel>

我的UserControl代码隐藏:

public sealed partial class RecomendationControl
{
    public RecommendedAnime Item => (RecommendedAnime) DataContext;

    public RecomendationControl()
    {
        InitializeComponent();
        DataContextChanged += (s, e) => Bindings.Update();
    }
}

2 个答案:

答案 0 :(得分:3)

这只是一种预感,但是你可以尝试在网格内的UserControl中包装所有内容吗?我记得我也遇到过这样的问题,并将其全部(包括VisualStateManager)放在网格中帮助。

答案 1 :(得分:-1)

同时尝试将MinWindowWidth设置为&#34; 1&#34;在NarrowState。根据我的经验,&#34; 0&#34;并不总是有效。