2016-05-18 49 views
0

我试图做到这一点,当窗口下降到一定大小以下时,按钮缩小。UWP/WinRT:是否可以使用AdaptiveTrigger更改样式?

这里是我的风格的代码:

<Style x:Key="AppBarButtonStyle" TargetType="AppBarButton"> 
    <Setter Property="Width" Value="68"/> 
</Style> 

我怎样才能使所有AppBarButtons成为64宽时窗低于720个像素?

回答

1

我们已经在另一个问题中讨论了这个问题,您的AppBarSeparators是在Pivot的DataTemplate中生成的。

仍然可以使用DataBinding与Converter来完成此操作,并且如果窗口大小在运行时可更改,则可能还需要使用INotifyPropertyChanged接口完成数据源类。

例如这里:

<Page.Resources> 
    <local:WidthConverter x:Key="cvt" /> 
</Page.Resources> 

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
    <Pivot x:Name="docPivot" ItemsSource="{x:Bind pivotlist}" SizeChanged="docPivot_SizeChanged"> 
     <Pivot.ItemTemplate> 
      <DataTemplate> 
       <Grid> 
        <Grid.RowDefinitions> 
         <RowDefinition Height="auto" /> 
         <RowDefinition Height="auto" /> 
        </Grid.RowDefinitions> 
        <StackPanel Orientation="Horizontal" Grid.Row="0"> 
         <AppBarButton Background="Red" Icon="Accept" Label="Accept" Width="{Binding WindowWidth, Converter={StaticResource cvt}}" /> 
        </StackPanel> 
        <StackPanel Orientation="Horizontal" 
         Grid.Row="1"> 
        </StackPanel> 
       </Grid> 
      </DataTemplate> 
     </Pivot.ItemTemplate> 
    </Pivot> 
</Grid> 

其他的东西都是一样的我在你的最后一个问题的答案。这里的WidthConverter是这样的:

public class WidthConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, string language) 
    { 
     double? width = (double?)value; 
     if (width <= 720) 
      return 64; 
     return 68; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, string language) 
    { 
     throw new NotImplementedException(); 
    } 
} 
+0

所以这是需要单独处理的宽度为使用样式每个项目,它不能直接改变风格改变,在一个多一起使用的所有项目清洁的方式? –

+0

@WilliamJones,我想不出任何干净的方式来管理这个。也许还有其他更简单的方法,但无论如何都应该在代码背后完成。 –

1

这应该是很容易,不知道你是否能一般设置样式,或者如果你是虽然限于命名元素,所以我会创建一个visualstatemanager,做那个,然后再进一步探讨我的选择:

<VisualStateManager.VisualStateGroups> 
      <VisualStateGroup x:Name="WindowStates"> 
       <VisualState x:Name="WideState"> 
        <VisualState.StateTriggers> 
         <AdaptiveTrigger MinWindowWidth="720" /> 
        </VisualState.StateTriggers> 
        <VisualState.Setters> 
         <Setter Target="Button1.Width" Value="100" />       
        </VisualState.Setters> 
       </VisualState> 
       <VisualState x:Name="NarrowState"> 
        <VisualState.StateTriggers> 
         <AdaptiveTrigger MinWindowWidth="0" /> 
        </VisualState.StateTriggers> 
        <VisualState.Setters> 
         <Setter Target="Button1.Width" Value="68" /> 
        </VisualState.Setters> 
       </VisualState> 
      </VisualStateGroup> 
     </VisualStateManager.VisualStateGroups> 
    </Grid> 
</Page> 
相关问题