gpt4 book ai didi

c# WPF,如何在单击按钮时扩展表单?

转载 作者:行者123 更新时间:2023-11-30 21:49:50 29 4
gpt4 key购买 nike

我正在开发一个 c# WPF 程序,我试图在单击按钮时扩展(变大)搜索表单(很像 Expander 函数,但整个表单)。

表格图片未展开: enter image description here

展开后的表格图片: enter image description here

我一开始就隐藏了控件,当单击展开按钮时,控件是可见的,但表单不跟随,我必须手动将表单放大。我尝试了所有方法,而且我已经搜索了几个小时。

我尝试了扩展功能。我尝试了自动 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>

谢谢大家//拉斯穆斯

最佳答案

哇哦,您可能应该重新编写整个布局,而不是使用 1 Grid 来放置所有内容并使用边距来放置它们。下面是一个示例,您可以如何使用简单的样式来切换表单下部的可见性。

Also Hidden for visibility 不同于 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>

关于c# WPF,如何在单击按钮时扩展表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36738377/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com