2015-05-08 25 views
4

如何在Windows 10中使用模板控制中的AdaptiveTrigger(我使用Windows 10 Pro Insider Preview Build 10074)。當窗口大小改變時,Window.Current.SizeChanged事件不會啓動。什麼是適當的方法來進行流體控制?這是我嘗試做,但沒有任何反應時,屏幕的尺寸變化:windows 10模板控制和AdaptiveTrigger

XAML template: 
<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:App1"> 

    <Style TargetType="local:CustomControl1" > 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="local:CustomControl1"> 
        <Border> 
         <VisualStateManager.VisualStateGroups> 
          <VisualStateGroup x:Name="VisualSizeStates"> 

           <VisualState x:Name="Small"> 
            <VisualState.StateTriggers> 
             <AdaptiveTrigger MinWindowHeight="0" MinWindowWidth="0" /> 
            </VisualState.StateTriggers> 
            <VisualState.Setters> 

             <Setter Target="Rect.Fill" Value="Green"/> 

            </VisualState.Setters> 
           </VisualState> 

           <VisualState x:Name="Big"> 
            <VisualState.StateTriggers> 
             <AdaptiveTrigger MinWindowHeight="1000" MinWindowWidth="1000" /> 

            </VisualState.StateTriggers> 
            <VisualState.Setters> 
             <Setter Target="Rect.Fill" Value="Blue"/> 
            </VisualState.Setters> 
           </VisualState> 
          </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 
         <Rectangle x:Name="Rect" Fill="Red" /> 
        </Border> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</ResourceDictionary> 

回答

2

我不認爲AdaptiveTriggers工作在那樣的風格。我得到它的唯一工作是直接在一個UserControl或頁面內的一個Grid中。我知道他們確實無法在DataTemplate中工作。我相信VisualStateManager必須位於控件內容之前。嘗試不同的方法是這樣的:

 <!--in app.xaml or something--> 
    <ControlTemplate x:Key="controlTemplate1" TargetType="MyControl"> 
     <Border Background="Green"/> 
    </ControlTemplate> 
    <ControlTemplate x:Key="controlTemplate2" TargetType="MyControl"> 
     <Border Background="Blue"/> 
    </ControlTemplate> 

    <!--in your page--> 
    <Grid> 
     <VisualStateManager.VisualStateGroups> 
      <VisualStateGroup x:Name="visualStateGroup" > 
       <VisualState> 
        <VisualState.StateTriggers> 
         <AdaptiveTrigger MinWindowWidth="720" /> 
        </VisualState.StateTriggers> 
        <VisualState.Setters> 
         <Setter Target="control.Template" Value="{StaticResource controlTemplate1}"/> 
        </VisualState.Setters> 
       </VisualState> 

       <VisualState> 
        <VisualState.StateTriggers> 
         <AdaptiveTrigger MinWindowWidth="0" /> 
        </VisualState.StateTriggers> 
        <VisualState.Setters> 
         <Setter Target="control.Template" Value="{StaticResource controlTemplate2}"/> 
        </VisualState.Setters> 
       </VisualState> 
      </VisualStateGroup> 
     </VisualStateManager.VisualStateGroups> 

     <MyControl x:Name="control" Template="{StaticResource controlTemplate1}"/> 
    </Grid> 

未測試,但讓我知道如何解決。

+0

謝謝!我發現觸發器在頁面級別上工作。因此,我創建了兩個可視狀態 - GreenVisualState和BlueVisualState以及一個用於更改視覺狀態的依賴屬性GoToVisualState。當頁面大小改變時,自適應觸發器將屬性設置爲期望的狀態。 – user3506220

+0

實際上,如果使用UserControl包裝DataTemplate內容,AdaptiveTriggers將在DataTemplate中工作。檢查這個答案:http://stackoverflow.com/questions/32088500/adaptivetrigger-and-datatemplate –

4

訣竅是,你必須將VisualStateManager與AdaptiveTrigger-s放入ControlTemplate的根控制中,否則它將無法工作。

這裏是例子:

Generic.xaml - >

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:AdaptiveLayoutExample"> 

    <Style TargetType="local:CustomControl1" > 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="local:CustomControl1"> 
        <Grid x:Name="RootGrid"> 
         <VisualStateManager.VisualStateGroups> 
          <VisualStateGroup> 
           <VisualState> 
            <VisualState.StateTriggers> 
             <AdaptiveTrigger MinWindowWidth="0"/> 
            </VisualState.StateTriggers> 
            <VisualState.Setters> 
             <Setter Target="RootGrid.Background" Value="Yellow"/> 
             <Setter Target="MyGrid.Background" Value="White"/> 
            </VisualState.Setters> 
           </VisualState> 
           <VisualState> 
            <VisualState.StateTriggers> 
             <AdaptiveTrigger MinWindowWidth="600"/> 
            </VisualState.StateTriggers> 
            <VisualState.Setters> 
             <Setter Target="RootGrid.Background" Value="Gray"></Setter> 
             <Setter Target="MyGrid.Background" Value="Red"></Setter> 
            </VisualState.Setters> 
           </VisualState> 
          </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 

         <Grid x:Name="MyGrid" Width="50" Height="50" Background="Black"/> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</ResourceDictionary> 

MainPage.xaml文件 - >

<Page 
    x:Class="AdaptiveLayoutExample.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:AdaptiveLayoutExample" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d"> 

    <Grid> 
     <local:CustomControl1 Width="100" Height="100"/> 
    </Grid> 
</Page>