2014-12-30 4 views
1

저는 WPF를 처음 사용하고 동적 뷰 생성 작업을합니다. 나는 모니터 경관 및/또는 초상에 기반한 UI를 수정할 필요가있는 시나리오를 가지고 있습니다. enter image description here가로 세로 방향의 WPF

모니터가 가로 모드 또는 세로 모드라는 것을 이미 알고 있습니다.

WPF에서도 가능합니까?

답변

3

이 가능하여 귀하의 재산에 레이아웃 컨테이너의 가시성을 바인딩 할 수 있습니다. 당신은 DataTrigger를 사용하여 그들 사이에 모두 배치 및 스위치를 구현하는보기를 만들 것입니다 :

<ContentControl> 
    <ContentControl.Style> 
     <Style TargetType="ContentControl"> 
      <Setter Property="Content"> 
       <Setter.Value> 
        <!-- Put your portrait layout here --> 
       </Setter.Value> 
      </Setter> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding IsLandscape}" Value="True"> 
        <Setter Property="Content"> 
         <Setter.Value> 
          <!-- Put your landscape layout here --> 
         </Setter.Value> 
        </Setter> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </ContentControl.Style>   
</ContentControl> 

은 {바인딩 IsLandscape} 표현을 사용하여을의 DataTrigger 뷰의 DataContext에의 IsLandscape 속성을 관찰한다. This is explained in detail on MSDN. 즉, 질문에서 언급 한 IsLandscape 속성이있는 개체에 대한보기의 DataContext 속성을 설정해야합니다. 전체 예제 :

  1. 빈 WPF 프로젝트를 새로 만듭니다.

  2. 업데이트합니다 MainWindow.xaml.cs :

    public MainWindow() 
    { 
        this.InitializeComponent(); 
    
        this.DataContext = this; // You would put a ViewModel here when using MVVM design pattern 
    } 
    
    public static readonly DependencyProperty IsLandscapeProperty 
        = DependencyProperty.Register("IsLandscape", 
                typeof (bool), 
                typeof (MainWindow)); 
    
    public bool IsLandscape 
    { 
        get { return (bool) GetValue(IsLandscapeProperty); } 
        set { SetValue(IsLandscapeProperty, value); } 
    } 
    
    private void ChangeOrientation(object sender, RoutedEventArgs e) 
    { 
        this.IsLandscape = !this.IsLandscape; 
    } 
    
  3. 업데이트합니다 MainWindow.xaml. 기본 그리드를 삭제하고 대신 넣어 : 당신의 빠른 응답

    <Window.Resources> 
        <Style TargetType="UserControl"> 
         <Setter Property="HorizontalContentAlignment" Value="Center" /> 
         <Setter Property="VerticalContentAlignment" Value="Center" /> 
         <Setter Property="Background" Value="#CCDDEE" /> 
         <Setter Property="Margin" Value="3" /> 
        </Style> 
    </Window.Resources> 
    
    <DockPanel> 
    
        <Button DockPanel.Dock="Bottom" Margin="5" Content="Change Orientation" 
          Click="ChangeOrientation" /> 
    
        <ContentControl> 
         <ContentControl.Style> 
          <Style TargetType="ContentControl"> 
           <Setter Property="Content"> 
            <Setter.Value> 
             <Grid> 
              <Grid.ColumnDefinitions> 
               <ColumnDefinition /> 
               <ColumnDefinition /> 
              </Grid.ColumnDefinitions> 
              <Grid.RowDefinitions> 
               <RowDefinition /> 
               <RowDefinition /> 
              </Grid.RowDefinitions> 
    
              <UserControl Content="Sub 1" /> 
              <UserControl Grid.Column="1" Content="Sub 2" /> 
              <UserControl Grid.Row="1" Grid.ColumnSpan="2" Content="Main" /> 
             </Grid> 
            </Setter.Value> 
           </Setter> 
           <Style.Triggers> 
            <DataTrigger Binding="{Binding IsLandscape}" Value="True"> 
             <Setter Property="Content"> 
              <Setter.Value> 
               <Grid> 
                <Grid.ColumnDefinitions> 
                 <ColumnDefinition /> 
                 <ColumnDefinition /> 
                </Grid.ColumnDefinitions> 
                <Grid.RowDefinitions> 
                 <RowDefinition /> 
                 <RowDefinition /> 
                </Grid.RowDefinitions> 
    
                <UserControl Grid.Column="1" Content="Sub 1" /> 
                <UserControl Grid.Column="1" Grid.Row="1" Content="Sub 2" /> 
                <UserControl Grid.RowSpan="2" Content="Main" /> 
               </Grid> 
              </Setter.Value> 
             </Setter> 
            </DataTrigger> 
           </Style.Triggers> 
          </Style> 
         </ContentControl.Style> 
        </ContentControl> 
    
    </DockPanel> 
    
+0

감사합니다. 내가 WPF에 익숙하지 않다고 말했기 때문에 Binding = "{바인딩 IsLandscape}"에 대한 간단한 브리핑을 부탁드립니다. – Chethan

+0

업데이트 된 답변보기. – Frank

1

예,이 UI를 모두 구현하고 VisualStateManager을 사용하여 표시 할 UI를 제어 할 수 있습니다.

또한 당신은 converter

관련 문제