扩展的Windows视图
本文关键字:视图 Windows 扩展 | 更新日期: 2023-09-27 18:02:55
我想做一个窗口,可以切换之间展开和紧凑的视图,只需点击一个按钮。
我想知道的是:有没有一种更简单的方法来调整大小和移动表单上的所有控件,而无需手动设置它们的位置和大小?
如果我能制作两种形式,一种是展开的形式,一种是紧凑的形式,然后在两种形式之间切换,那肯定会更容易。但是我不知道如何让它们引用相同的代码
您是否看过流体宽度,并为扩展/紧凑模式创建属性?我已经附上了一个简单的例子,如何有你的视图的部分绑定到窗口的"模式"。
这项技术也适用于MVVM。我做了一个"奇怪的"混合,而不是实现一个DependencyProperty
的可绑定属性,我已经绑定到主窗口,并使用它作为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;
}
}
}