【问题标题】:DockPanel resizing items wpfDockPanel 调整项目大小 wpf
【发布时间】:2015-09-17 13:40:38
【问题描述】:

我知道有一些例子,但我想知道是否有替代解决方案,所以这里是:)。

我有一个使用停靠面板的 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>

编辑

我想要的是标签和按钮即使现在也适合窗口,因为我增加了窗口的大小以便标签横跨窗口

【问题讨论】:

  • 听起来你想在窗口调整大小时保持布局。您能否向我们提供有关该布局的更多详细信息?我不同意使用 Grid 会阻止您删除元素。您可以发布一些复制该问题的 xaml 吗?
  • 是的,如果你愿意,我可以给你 XAML 和 C#?也许我在尝试删除元素时做错了,但我今天试了一下,但由于某种原因它没有用
  • 请提供屏幕截图,以便我们知道我们在说什么
  • @Gusdor 如果我要添加一个网格并且仍然能够删除标签、按钮和列(例如岩石属性),我将如何使用网格?

标签: c# wpf dockpanel


【解决方案1】:

据我了解,有您的要求:

  • 随着窗口大小调整而缩放的布局
  • 移除元素的能力

这个解决方案是一个巨大的简化,但它应该让你走上正轨。

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);
        }
    }
}

【讨论】:

  • 是的,这正是我的要求,谢谢,我想你也可以对后面代码中的网格列做同样的事情?
猜你喜欢
  • 2011-11-13
  • 1970-01-01
  • 2011-04-24
  • 2021-04-15
  • 1970-01-01
  • 2018-05-05
  • 1970-01-01
  • 1970-01-01
  • 2011-07-15
相关资源
最近更新 更多