WPF中的横向纵向

我是WPF新手并致力于动态视图创建。 我有一个场景,我需要根据监视器格局和/或肖像修改我的UI,如 在此处输入图像描述

我已经拥有告诉我监视器处于横向或纵向模式的属性。

这可能在WPF中吗?

这个有可能。 您将使用DataTrigger创建一个实现布局和在它们之间切换的视图:

     

使用{Binding IsLandscape}表达式,DataTrigger会观察视图的DataContext的IsLandscape属性。 这在MSDN上有详细解释 。 这意味着您应该将视图的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 。 删除默认网格并改为:

                

是的,您可以实现此UI并使用VisualStateManager来控制要显示的UI。

您还可以使用转换器将布局容器的可见性绑定到您的属性