我知道有一些例子,但我想知道是否有替代解决方案,所以这里:)。
我有一个使用dockpanel的wpf应用程序,并且标签顶部显示为"标题"。一切都很好,直到我最大化窗口。标签没有调整大小,是否有一种方法可以不使用网格来完成这项工作?我尝试使用网格然后它禁用了删除不幸停靠的项目的能力:(。任何帮助都会很棒。我也尝试使用网格分割器但是没有成功
修改
<Window x:Class="DockPanelTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="1500">
<Window.Resources>
<LinearGradientBrush x:Key="headerBackground" StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="#FFEFEEEE" Offset="0"/>
<GradientStop Color="#E7E7E7E7" Offset="1"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="selectedHeaderBackground" StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="#F6CD1D" Offset="0"/>
<GradientStop Color="#EBA32A" Offset="1"/>
</LinearGradientBrush>
</Window.Resources>
<DockPanel Name="myDockPanel">
<Label Width="Auto" HorizontalAlignment="Stretch" Name="RockLabel" Content="Rock Properties" Margin="0,0,1,1" DockPanel.Dock="Left" Background="{StaticResource headerBackground}" VerticalAlignment="Top" MouseUp="RockLabel_MouseUp" />
<Button Width="Auto" Name="CloseRock" HorizontalAlignment="Stretch" Margin="0,0,1,1" Content="X" VerticalAlignment="Top" Height="26" Background="{StaticResource headerBackground}" Click="CloseRock_Click" />
<Label Width="Auto" Name="ContactsLabel" Margin="0,0,1,1" Content="Contacts" DockPanel.Dock="Left" Background="{StaticResource headerBackground}" VerticalAlignment="Top" MouseUp="ContactsLabel_MouseUp" HorizontalAlignment="Stretch" />
<Button Width="Auto" Name="CloseContacts" Margin="0,0,1,1" Content="X" VerticalAlignment="Top" Height="26" Background="{StaticResource headerBackground}" HorizontalAlignment="Stretch" Click="CloseContacts_Click" />
<Label Width="Auto" Name="FluidLabel" Margin="0,0,1,1" Content="Fluid Properties" DockPanel.Dock="Left" Background="{StaticResource headerBackground}" VerticalAlignment="Top" MouseUp="FluidLabel_MouseUp" HorizontalAlignment="Stretch" />
<Button Width="Auto" Name="CloseFluid" Margin="0,0,1,1" Content="X" VerticalAlignment="Top" Background="{StaticResource headerBackground}" Height="26" Click="CloseFluid_Click" HorizontalAlignment="Stretch" />
<Label Width="Auto" Name="RegionsLabel" Margin="0,0,1,1" Content="Regions" DockPanel.Dock="Left" Background="{StaticResource headerBackground}" VerticalAlignment="Top" MouseUp="RegionsLabel_MouseUp" HorizontalAlignment="Stretch" />
<Button Width="Auto" Name="CloseRegions" Margin="0,0,1,1" Content="X" VerticalAlignment="Top" Background="{StaticResource headerBackground}" Height="26" Click="CloseRegions_Click" HorizontalAlignment="Stretch"/>
<Label Width="Auto" Name="ProbabilitiesLabel" Margin="0,0,1,1" Content="Probabilities" DockPanel.Dock="Left" Background="{StaticResource headerBackground}" VerticalAlignment="Top" MouseUp="ProbabilitiesLabel_MouseUp" HorizontalAlignment="Stretch"/>
<Button Width="Auto" Name="CloseProbabilities" Margin="0,0,1,1" Content="X" VerticalAlignment="Top" Background="{StaticResource headerBackground}" Height="26" Click="CloseProbabilities_Click" HorizontalAlignment="Stretch"/>
<Label Width="Auto" Name="EconomicsLabel" Margin="0,0,1,1" Content="Economics" DockPanel.Dock="Left" Background="{StaticResource headerBackground}" VerticalAlignment="Top" MouseUp="EconomicsLabel_MouseUp"/>
<Button Width="Auto" Name="CloseEconomics" Margin="0,0,1,1" Content="X" VerticalAlignment="Top" Background="{StaticResource headerBackground}" Height="26" HorizontalAlignment="Right" Click="CloseEconomics_Click"/>
<Button Width="Auto" Name="RestoreButton" Content="Restore" Height="32" VerticalAlignment="Bottom" HorizontalAlignment="Stretch" Click="RestoreButton_Click"/>
<Button Width="Auto" Name="RestoreLabels" Content="Restore Labels" Height="32" VerticalAlignment="Center" HorizontalAlignment="Center" Click="RestoreLabels_Click"/>
</DockPanel>
我想要的是标签和按钮适合窗口,因为我增加了窗口的大小,因此标签在窗口上方
答案 0 :(得分:2)
根据我的理解,有您的要求:
这个解决方案是一个很大的简化,但它应该让你走上正确的轨道。
<Window x:Class="ResizeSample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ResizeSample"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="Grid_ButtonHost">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Click="Button_Remove_Click">Click to remove</Button>
<Button Click="Button_Remove_Click" Grid.Column="1" Grid.Row="1">Click to remove</Button>
<Button Click="Button_Remove_Click" Grid.Column="2" Grid.Row="2">Click to remove</Button>
</Grid>
</Window>
namespace ResizeSample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Remove_Click(object sender, RoutedEventArgs e)
{
// we gave the hosting grid a name, so now we can remove the sender of the click event from the grid.
// remember to cast the sender to UIElement!
Grid_ButtonHost.Children.Remove((UIElement)sender);
}
}
}