2013-11-15 49 views
0

我想在單擊按鈕時在程序中最大化wpf窗口,但我寫的代碼卻沒有最大化。在隱藏某些控件時最大化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設置的空白。

我該如何最大限度地發揮它,出了什麼問題?

(你可以在第一個圖片中看到我的窗口,當我在左邊按鈕角落點擊最大化和隱藏一些控件,它並不能完全最大化。)

enter image description here enter image description here

+1

我不知道你在說什麼。張貼你得到的東西和你需要的東西的截圖。 –

回答

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; 
+1

@abdolah我不想過於煩瑣,但理想情況下,您最終應該將'private GridLength _origWidth'更改爲依賴項prop(或至少一個屬性)等。 – narendra