当我点击一个按钮时,我想在我的程序中最大化一个wpf窗口,我写了这段代码,但它没有最大化。
private void ShowHideMotorsButtonClick(object sender, RoutedEventArgs e)
{
Motor1WFH.Visibility = Visibility.Hidden;
Motor2WFH.Visibility = Visibility.Hidden;
MainTabControl.Margin = new Thickness(12, 12, 165, 12);
this.WindowState = System.Windows.WindowState.Maximized;
}
xaml代码:
<Grid >
<WindowsFormsHost Height="200" HorizontalAlignment="Right" Margin="0,43,12,0" VerticalAlignment="Top" Width="300" Name="Motor1WFH">
<apt:MG17Motor x:Name="Motor1" />
</WindowsFormsHost>
<WindowsFormsHost Height="200" HorizontalAlignment="Right" Margin="0,249,11,0" VerticalAlignment="Top" Width="300" Name="Motor2WFH">
<apt:MG17Motor x:Name="Motor2" />
</WindowsFormsHost>
<TabControl Margin="12,12,318,12" Name="MainTabControl">
...
但以下代码有效:
private void ShowHideMotorsButtonClick(object sender, RoutedEventArgs e)
{
Motor1WFH.Visibility = Visibility.Hidden;
Motor2WFH.Visibility = Visibility.Hidden;
this.WindowState = System.Windows.WindowState.Maximized;
}
当我没有为MainTabControl设置保证金时。
我如何最大化它以及出了什么问题?
(正如你在第一张照片中看到的那样,我有窗口,当我点击左边的按钮以最大化它并隐藏一些控件时,它并没有完全最大化。)
答案 0 :(得分:0)
这是一个可能有用的简单示例,将左侧控件放在自己的网格中。首先是xaml:
<Grid Name="MainGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<TabControl>
<TabItem Header="+"/>
<TabItem Header="-"/>
<TabItem Header="Other"/>
</TabControl>
</Grid>
<Grid Grid.Column="1">
<TabControl>
<TabItem Header="Option 1"/>
<TabItem Header="Option 2"/>
<TabItem Header="Option 3"/>
</TabControl>
<Button Background="Black" Width="30" Height="20"
HorizontalAlignment="Right" VerticalAlignment="Top" Click="OnExpBtnClick"/>
</Grid>
</Grid>
然后是背后的代码:
private void OnExpBtnClick(object sender, RoutedEventArgs e)
{
if (_exp)
{
_exp = false;
MainGrid.ColumnDefinitions[0].Width = _origWidth;
}
else
{
_exp = true;
_origWidth = MainGrid.ColumnDefinitions[0].Width;
MainGrid.ColumnDefinitions[0].Width = new GridLength(0.0, GridUnitType.Pixel);
}
}
以下是课程范围
private bool _exp;
private GridLength _origWidth;