gpt4 book ai didi

c# - 通过 visualState 触发器更改 WrapGrid 的 maximumRowsOrColums 值不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 15:37:12 30 4
gpt4 key购买 nike

我正在为我的 win 10 通用应用程序制作自适应布局。出于某种原因,当我尝试在 visualState 触发器中更改 wrapGrid 的 maximumRowsOrColumns 值时,它无法正常工作。

好吧,如果用户更改窗口大小,它实际上会起作用,但是在启动程序时,adaptiveTrigger 似乎对该值没有影响。

这是一些代码:

<Hub>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState x:Name="wideView">
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="720" />
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target="WrapPanel.MaximumRowsOrColumns" Value="3" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="narrowView">
<VisualState.Setters>
<Setter Target="WrapPanel.MaximumRowsOrColumns" Value="2" />
</VisualState.Setters>
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="0" />
</VisualState.StateTriggers>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>

<HubSection>
<DataTemplate>
<GridView>
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<WrapGrid Orientation="Horizontal"
x:Name="WrapPanel"/>
</ItemsPanelTemplate>
</GridView.ItemsPanel>
<GridViewItem>
<Rectangle Width="250" Height="100" Fill="Red" Margin="6" />
</GridViewItem>
<GridViewItem>
<Rectangle Width="250" Height="100" Fill="Yellow" Margin="6" />
</GridViewItem>
<GridViewItem>
<Rectangle Width="250" Height="100" Fill="Green" Margin="6" />
</GridViewItem>
<GridViewItem>
<Rectangle Width="250" Height="100" Fill="Blue" Margin="6" />
</GridViewItem>
</GridView>
</DataTemplate>
</HubSection>
</Hub>

最佳答案

如果您希望触发器在启动时起作用,您可以尝试强制应用程序以特定的宽度和高度启动。这样你的视觉状态就可以被触发。下面是您需要放入 yourfile.xaml.cs 中的片段:

ApplicationView.PreferredLaunchWindowingMode != ApplicationViewWindowingMode.Auto;
ApplicationView.PreferredLaunchViewSize = new Size { Width = 600, Height = 500 };
ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;

另外一个好的编码实践是从 > 标签中取出 Visual State Manager XAML,并将它们放在代码的末尾(但在 标签内)

    <Hub>
<HubSection x:Name="HubSection" Header="Test">
<DataTemplate>
<GridView>
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<WrapGrid Orientation="Horizontal"
x:Name="WrapPanel1" MaximumRowsOrColumns="3"/>
</ItemsPanelTemplate>
</GridView.ItemsPanel>
<GridViewItem >
<Rectangle Width="250" Height="100" Fill="Red" Margin="6" />
</GridViewItem>
<GridViewItem>
<Rectangle Width="250" Height="100" Fill="Yellow" Margin="6" />
</GridViewItem>
<GridViewItem>
<Rectangle Width="250" Height="100" Fill="Green" Margin="6" />
</GridViewItem>
<GridViewItem>
<Rectangle Width="250" Height="100" Fill="Blue" Margin="6" />
</GridViewItem>
<GridViewItem>
<Rectangle Width="250" Height="100" Fill="Orange" Margin="6" />
</GridViewItem>
</GridView>
</DataTemplate>
</HubSection>
</Hub>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState x:Name="wideView">
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="700" />
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target="WrapPanel1.MaximumRowsOrColumns" Value="3"/>
<Setter Target="HubSection.Header" Value="WideView" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="narrowView">
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="0" />
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target="WrapPanel1.MaximumRowsOrColumns" Value="2" />
<Setter Target="HubSection.Header" Value="Vertical" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>

关于c# - 通过 visualState 触发器更改 WrapGrid 的 maximumRowsOrColums 值不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31429529/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com