Jor*_*sch 7 c# wpf xaml resize
我有一个带有两个用户控件的wpf窗口,其中第二个只在需要时显示.我只在XAML中设置窗口的MinWidth,MinHeight是通过数据绑定提供的,具体取决于是否显示第二个用户控件.现在:如何在运行时将窗口大小设置为与MinWidth/Height不同的值.我尝试在Show()之后的Show()之前设置各种事件(Initialized,Loaded等)中的值.我尝试使用和不使用UpdateLayout(),我尝试通过数据绑定设置高度/宽度.什么都行不通!但是,当我调试方法时,我看到窗口的高度/宽度属性设置为预期值,但ActualHeight/Width保持不变.我认为这将是一个小包,但事实证明它不是(对我而言).你的任何帮助.
你试过试试吗?
Application.Current.MainWindow.Height = 100;
Run Code Online (Sandbox Code Playgroud)
简短补充:我刚刚在代码中做了一个简短的测试:
public partial class MainWindow : Window, INotifyPropertyChanged
{
private int _height;
public int CustomHeight
{
get { return _height; }
set
{
if (value != _height)
{
_height = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("CustomHeight"));
}
}
}
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
CustomHeight = 500;
}
public event PropertyChangedEventHandler PropertyChanged;
private void Button_Click(object sender, RoutedEventArgs e)
{
CustomHeight = 100;
}
}
Run Code Online (Sandbox Code Playgroud)
和XAML:
<Window x:Class="WindowSizeTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="{Binding CustomHeight, Mode=TwoWay}" Width="525">
<Grid>
<Button Click="Button_Click">Test</Button>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
单击按钮设置窗口高度.那是你在找什么?
如果有人仍在为此苦苦挣扎,那么您唯一要做的就是:
如果要调整主窗口的大小,只需编写以下代码。
Application.Current.MainWindow.Height = 420;
Run Code Online (Sandbox Code Playgroud)
如果要调整主窗口以外的新窗口的大小,只需在新窗口的 .cs 文件中编写以下代码即可。
Application.Current.MainWindow = this;
Application.Current.MainWindow.Width = 420;
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你。
| 归档时间: |
|
| 查看次数: |
24615 次 |
| 最近记录: |