Skip to main content
 首页 » 编程设计

c#之WPF 中的横向-纵向方向

2024年02月06日13pengyingh

我是 WPF 新手,致力于动态 View 创建。我有一个场景,我需要根据监视器横向和/或纵向修改我的 UI,比如

我已经有属性告诉我显示器处于横向或纵向模式。

这在 WPF 中可能吗?

请您参考如下方法:

这是可能的。您将创建一个实现这两种布局并使用 DataTrigger 在它们之间切换的 View :

<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> 

使用 {Binding IsLandscape} 表达式,DataTrigger 观察 View 的 DataContext 的 IsLandscape 属性。 This is explained in detail on MSDN .这意味着您应该将 View 的 DataContext 属性设置为具有您在问题中提到的 IsLandscape 属性的对象。完整示例:

  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。删除默认的 Grid 并将其改为:

    <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>