扩展的Windows视图

时间:2014-05-13 00:02:15

标签: c# wpf

我想创建一个窗口,只需单击一个按钮即可在展开视图和紧凑视图之间切换。

我想知道的是:是否有更简单的方法来调整窗体上的所有控件的大小并移动而无需手动设置其位置和大小?

如果我可以制作两种形式,扩展和紧凑形式,然后在两种形式之间切换,那肯定会更容易。但我无法弄清楚如何让它们引用相同的代码。

1 个答案:

答案 0 :(得分:1)

您是否查看过流体宽度,并为Expanded / Compact模式创建属性?我附上了一个简单的示例,说明如何将视图的某些部分绑定到"模式"窗户。

这种技术也适用于MVVM。我做了一个很奇怪的"混合,我没有为可绑定属性实现DependencyProperty,而是绑定到MainWindow并将其用作ViewModel。您可以选择实现绑定的方式。

<强> MainWindow.xaml

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        mc:Ignorable="d"
        xmlns:wpfApplication2="clr-namespace:WpfApplication2"
        d:DataContext="{d:DesignInstance Type=wpfApplication2:MainWindow, IsDesignTimeCreatable=True}"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <StackPanel Background="LightGray">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Really Long Text Example" Visibility="{Binding ExpandedVisibility}" />
                <TextBlock Text="Compact Text"  Visibility="{Binding CompactVisibility}" />
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Really Long Text Example" Visibility="{Binding ExpandedVisibility}" />
                <TextBlock Text="Compact Text"  Visibility="{Binding CompactVisibility}" />
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Really Long Text Example" Visibility="{Binding ExpandedVisibility}" />
                <TextBlock Text="Compact Text"  Visibility="{Binding CompactVisibility}" />
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Really Long Text Example" Visibility="{Binding ExpandedVisibility}" />
                <TextBlock Text="Compact Text"  Visibility="{Binding CompactVisibility}" />
            </StackPanel>
        </StackPanel>
        <Grid Grid.Column="1">
            <Border VerticalAlignment="Top" Height="50" Visibility="{Binding ExpandedVisibility}">
                <TextBlock Text="Non Compact Section Only" Background="#FFFFF593"></TextBlock>
            </Border>
            <Button Content="Compact / Expand" Click="Button_Click" HorizontalAlignment="Center" VerticalAlignment="Center" />
        </Grid>
        <Grid Grid.Column="2" Width="150">
            <Rectangle Visibility="{Binding ExpandedVisibility}" Fill="Red" />
            <Rectangle Visibility="{Binding CompactVisibility}" Fill="Blue" />
        </Grid>
    </Grid>
</Window>

<强> MainWindow.cs

using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;

namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        private bool _isExpandedMode;
        private Visibility _compactVisibility;

        public bool IsExpandedMode
        {
            get { return _isExpandedMode; }
            set
            {
                _isExpandedMode = value;
                OnPropertyChanged();
                OnPropertyChanged("ExpandedVisibility");
                OnPropertyChanged("CompactVisibility");
            }
        }

        public Visibility ExpandedVisibility
        {
            get { return IsExpandedMode ? Visibility.Visible : Visibility.Collapsed; }
        }

        public Visibility CompactVisibility
        {
            get { return !IsExpandedMode ? Visibility.Visible : Visibility.Collapsed; }
        }

        public MainWindow()
        {
            InitializeComponent();
            this.IsExpandedMode = true;
            this.DataContext = this;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            IsExpandedMode = !IsExpandedMode;
        }
    }
}