2016-04-20 71 views
0

我正在處理一個c#WPF程序,並試圖使搜索表單展開(變大),當單擊一個按鈕(很像擴展函數,但整個表單)。c#WPF,如何在按鈕單擊時展開窗體?

圖片形式的不展開: enter image description here

圖片形式的展開方式: enter image description here

我做了隱藏,以開始與控制,當展開按鈕被點擊,控制是可見,但形式並沒有遵循,我必須使手動形式更大。我嘗試了一切,我一直在尋找幾個小時。

我試過了擴展功能。我試過auto sizeToContent函數。

這裏是XAML代碼:

<Window x:Class="MockUps.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Search" Icon="C:\Users\rasmus\Desktop\Lejos.dk\LejosLogo.jpg" WindowStartupLocation="Manual" Height="319" Width="480"> 
<Grid AllowDrop="False" Height="auto" Width="480" SizeChanged="Grid_SizeChanged" Margin="2,0,1,-151"> 
    <ComboBox HorizontalAlignment="Left" Margin="184,24,0,0" VerticalAlignment="Top" Width="120"> 
     <ComboBoxItem Content="Electrician"/> 
     <ComboBoxItem Content="Metalworker"/> 
     <ComboBoxItem Content="Landscaper"/> 
     <ComboBoxItem Content="Painter"/> 
     <ComboBoxItem Content="Plumber"/> 
     <ComboBoxItem Content="Driver"/> 
     <ComboBoxItem Content="Craneworker"/> 
     <ComboBoxItem Content="Machineworker"/> 
    </ComboBox> 
    <Label Content="Job Type" HorizontalAlignment="Left" Margin="47,20,0,0" VerticalAlignment="Top"/> 
    <Label Content="Certificate(s)" HorizontalAlignment="Left" Margin="28,60,0,0" VerticalAlignment="Top"/> 
    <Label Content="Nationality" HorizontalAlignment="Left" Margin="36,140,0,0" VerticalAlignment="Top"/> 
    <Label Content="Language(s)" HorizontalAlignment="Left" Margin="30,100,0,0" VerticalAlignment="Top"/> 
    <ComboBox HorizontalAlignment="Left" Margin="184,64,0,0" VerticalAlignment="Top" Width="120"> 
     <ComboBoxItem Content="Welding"/> 
     <ComboBoxItem Content="Offshore"/> 
     <ComboBoxItem Content="Crane C"/> 
     <ComboBoxItem Content="Crane B"/> 
     <ComboBoxItem Content="Crane A"/> 
     <ComboBoxItem Content="Arc-Welding/Lysbuesvejsning"/> 
     <ComboBoxItem Content="TIG-Welding"/> 
     <ComboBoxItem Content="Gas-Welding"/> 
    </ComboBox> 
    <ComboBox HorizontalAlignment="Left" Margin="184,104,0,0" VerticalAlignment="Top" Width="120"> 
     <ComboBoxItem Content="Danish"/> 
     <ComboBoxItem/> 
     <ComboBoxItem Content="Swedish"/> 
     <ComboBoxItem Content="Norwegian"/> 
     <ComboBoxItem Content="Deutsch"/> 
     <ComboBoxItem Content="Polish"/> 
     <ComboBoxItem Content="English"/> 
    </ComboBox> 
    <ComboBox HorizontalAlignment="Right" Margin="0,104,27,0" VerticalAlignment="Top" Width="120"> 
     <ComboBoxItem Content="Danish"/> 
     <ComboBoxItem Content="Swedish"/> 
     <ComboBoxItem Content="Norwegian"/> 
     <ComboBoxItem Content="Deutsch"/> 
     <ComboBoxItem Content="Polish"/> 
     <ComboBoxItem Content="English"/> 
    </ComboBox> 
    <ComboBox HorizontalAlignment="Left" Margin="184,144,0,0" VerticalAlignment="Top" Width="120"> 
     <ComboBoxItem Content="Danish"/> 
     <ComboBoxItem Content="Swedish"/> 
     <ComboBoxItem Content="Norwegian"/> 
     <ComboBoxItem Content="German"/> 
     <ComboBoxItem Content="Polish"/> 
    </ComboBox> 
    <Label Content="Drivers License" HorizontalAlignment="Left" Margin="15,180,0,0" VerticalAlignment="Top"/> 
    <ComboBox HorizontalAlignment="Left" Margin="184,180,0,0" VerticalAlignment="Top" Width="120"> 
     <ComboBoxItem Content="B - Car"/> 
     <ComboBoxItem Content="C1 - Small Truck"/> 
     <ComboBoxItem Content="C - Big Truck"/> 
     <ComboBoxItem Content="D1 - Small Bus"/> 
     <ComboBoxItem Content="D - Big Bus"/> 
     <ComboBoxItem Content="E - Trailer"/> 
    </ComboBox> 
    <ComboBox HorizontalAlignment="Right" Margin="0,64,27,0" VerticalAlignment="Top" Width="120"> 
     <ComboBoxItem Content="Welding"/> 
     <ComboBoxItem Content="Offshore"/> 
     <ComboBoxItem Content="Crane C"/> 
     <ComboBoxItem Content="Crane B"/> 
     <ComboBoxItem Content="Crane A"/> 
     <ComboBoxItem Content="Arc-Welding/Lysbuesvejsning"/> 
     <ComboBoxItem Content="TIG-Welding"/> 
     <ComboBoxItem Content="Gas-Welding"/> 
    </ComboBox> 
    <Button Content="Search" HorizontalAlignment="Left" Margin="137,257,0,0" VerticalAlignment="Top" Width="75"/> 
    <Label Content="Experience" HorizontalAlignment="Left" Margin="37,220,0,0" VerticalAlignment="Top"/> 
    <ComboBox HorizontalAlignment="Left" Margin="184,220,0,0" VerticalAlignment="Top" Width="120"> 
     <ComboBoxItem Content="Min. 5 Years"/> 
     <ComboBoxItem Content="Min. 10 Years"/> 
     <ComboBoxItem Content="Min. 15 Years"/> 
     <ComboBoxItem Content="Min. 20 Years"/> 
    </ComboBox> 
    <Image HorizontalAlignment="Right" Height="96" VerticalAlignment="Top" Width="120" Margin="0,144,27,0" Source="C:\Users\rasmus\Desktop\Lejos.dk\LejosLogo.jpg" Stretch="Fill"/> 
    <Button Content="Expand" HorizontalAlignment="Left" Margin="268,257,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/> 
    <Label x:Name="lblGender" Content="Gender" HorizontalAlignment="Left" Margin="55,300,0,0" VerticalAlignment="Top" Visibility="Hidden"/> 
    <Label x:Name="lblMinAge" Content="Min. Age" HorizontalAlignment="Left" Margin="47,340,0,0" VerticalAlignment="Top" Visibility="Hidden"/> 
    <Label x:Name="lblMaxAge" Content="Max. Age" HorizontalAlignment="Left" Margin="47,380,0,0" VerticalAlignment="Top" Visibility="Hidden"/> 
    <ComboBox x:Name="CBGender" HorizontalAlignment="Left" Margin="184,304,0,0" VerticalAlignment="Top" Width="120" Visibility="Hidden"> 
     <ComboBoxItem Content="Male"/> 
     <ComboBoxItem Content="Female"/> 
    </ComboBox> 
    <ComboBox x:Name="CBMinAge" HorizontalAlignment="Left" Margin="184,344,0,0" VerticalAlignment="Top" Width="120" SelectionChanged="CBMinAge_SelectionChanged" Visibility="Hidden"/> 
    <ComboBox x:Name="CBMaxAge" HorizontalAlignment="Left" Margin="184,384,0,0" VerticalAlignment="Top" Width="120" Visibility="Hidden"/> 

</Grid> 

謝謝你們 //拉斯穆斯

+2

顯示xaml佈局。 –

+0

控件很可能會變得可見,但它們在框架之外。相應地調整窗口高度以增加框架。 – Sami

+0

嗨薩米, 是的,我知道我可以調整高度以適應其他控件,但重點是我希望它很小,只有在用戶需要使用高級搜索時纔會變大。如果根據內容的自動尺寸工作,將是如此聰明,但它不.. .. – RasmusPaulsen

回答

2

哇哦,你應該重新寫你的整個佈局,而不是使用1個格來放置所有內容並使用邊距放置它們。下面是一個示例,您可以使用簡單的樣式來切換窗體下半部分的可見性。

隱藏可見性與Collapsed不同。隱藏的元素將爲他們保留佈局空間。摺疊不會。

<Window x:Class="MockUps.MainWindow" 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       Title="Search" 
       SizeToContent="WidthAndHeight"> 
    <StackPanel> 
     <Grid> 
      <ToggleButton x:Name="ExpandButton" Content="Expand"/> 
     </Grid> 

     <Grid> 
      <Grid.Style> 
       <Style TargetType="{x:Type Grid}"> 
        <Setter Property="Visibility" Value="Collapsed"/> 
        <Style.Triggers> 
         <DataTrigger Binding="{Binding ElementName=ExpandButton, Path=IsChecked}" Value="True"> 
          <Setter Property="Visibility" Value="Visible"/> 
         </DataTrigger> 
        </Style.Triggers> 
       </Style> 
      </Grid.Style> 

      <Label Content="Gender"/> 
      <Label Content="Min. Age"/> 
      <Label Content="Max. Age"/> 

      <ComboBox> 
       <ComboBoxItem Content="Male"/> 
       <ComboBoxItem Content="Female"/> 
      </ComboBox> 

      <ComboBox/> 

      <ComboBox/> 
     </Grid> 
    </StackPanel> 
</Window> 
+0

謝謝Janne,我重新開始,試着說出你的想法。 – RasmusPaulsen

+0

對於上半部分,您似乎有3列x 7行網格,因此添加rowdefinitions和columndefinitions,然後將控件分配到行和列,並使用Padding在它們周圍添加填充。 –

相關問題