DockPanel 调整项目大小 wpf
DockPanel resizing items wpf
我知道有几个例子,但我想知道是否有替代解决方案,所以这里是:)。
我有一个使用停靠面板的 wpf 应用程序,并且标签在顶部显示为 "headers"。一切都很好,直到我最大化 window。标签不会调整大小,有没有办法不使用网格来完成这项工作?我尝试使用网格,但不幸的是,它使我无法删除停靠的项目:(。任何帮助都会很棒。我也尝试过使用网格分离器,但都没有成功
编辑
<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>
编辑
我想要的是标签和按钮适合 window 即使现在我增加了 window 的大小所以标签横跨 window
据我了解,您的要求是:
- 随着 window 调整大小而缩放的布局
- 移除元素的能力
这个解决方案是一个巨大的简化,但它应该会让您走上正轨。
XAML
<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);
}
}
}
我知道有几个例子,但我想知道是否有替代解决方案,所以这里是:)。
我有一个使用停靠面板的 wpf 应用程序,并且标签在顶部显示为 "headers"。一切都很好,直到我最大化 window。标签不会调整大小,有没有办法不使用网格来完成这项工作?我尝试使用网格,但不幸的是,它使我无法删除停靠的项目:(。任何帮助都会很棒。我也尝试过使用网格分离器,但都没有成功
编辑
<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>
编辑
我想要的是标签和按钮适合 window 即使现在我增加了 window 的大小所以标签横跨 window
据我了解,您的要求是:
- 随着 window 调整大小而缩放的布局
- 移除元素的能力
这个解决方案是一个巨大的简化,但它应该会让您走上正轨。
XAML
<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);
}
}
}