UWP UserControl 不会自适应大小
在一般的Page里面,我们通过VisualStateManager,可以根据窗体的宽度,来调整一些控件大小。
<VisualStateManager.VisualStateGroups> <VisualStateGroup x:Name="WindowStates"> <VisualState x:Name="PanoramicState"> <VisualState.StateTriggers> <AdaptiveTrigger MinWindowWidth="1024"/> </VisualState.StateTriggers> <VisualState.Setters> <Setter Target="row1.Height" Value="0"/> <Setter Target="gridX.Visibility" Value="Collapsed"/> </VisualState.Setters> </VisualState> <VisualState x:Name="WideState"> <VisualState.StateTriggers> <AdaptiveTrigger MinWindowWidth="640"/> </VisualState.StateTriggers> <VisualState.Setters> <Setter Target="column1.Width" Value="0"/> <Setter Target="gridY.Visibility" Value="Collapsed"/> </VisualState.Setters> </VisualState> <VisualState x:Name="NarrowState"> <VisualState.StateTriggers> <AdaptiveTrigger MinWindowWidth="0"/> </VisualState.StateTriggers> <VisualState.Setters> <Setter Target="column1.Width" Value="0"/> <Setter Target="gridY.Visibility" Value="Collapsed"/> </VisualState.Setters> </VisualState> </VisualStateGroup> </VisualStateManager.VisualStateGroups>
挡在UserControl里面使用的时候,需要注意下VisualStateManager需要放在根控件的里面,比如定义Grid
<Grid> <VisualStateManager.VisualStateGroups> <VisualStateManager.VisualStateGroups/> 其他控件。。。 <Grid/>
这样自适应才会起作用。
查了Stackoverflow才知道的。https://stackoverflow.com/questions/38175647/uwp-visualstates-in-usercontrol