【问题标题】:Set Dockpanel width and height as same as Window width and height in wpf将 Dockpanel 的宽度和高度设置为与 wpf 中的窗口宽度和高度相同
【发布时间】:2017-04-24 05:31:29
【问题描述】:

我在这个窗口中创建了主窗口,创建了用于在主窗口中绑定用户控件值的停靠面板,如下所示,

<Window x:Class="WpfApplication2.DMMainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       ResizeMode="NoResize"
       WindowState="Maximized" 
       WindowStyle="None"
       WindowStartupLocation="CenterScreen"
       Height="{Binding SystemParameters.PrimaryScreenHeight}"
       Width="{Binding SystemParameters.PrimaryScreenWidth}">
    <DockPanel Width="1254" Height="1200" HorizontalAlignment="Left" Margin="0,0,0,0" x:Name="mainPanel" VerticalAlignment="Top" />
</Window>

在这段代码中,您可以看到停靠面板的给定宽度和高度。我需要这个高度和宽度需要与窗口宽度相同。我已经使用了实际的宽度和高度以及 sizetocontent 但没有按预期发生。请提出您的建议。

【问题讨论】:

  • 当用作*容器时,DockPanel 会自动填充Window 父级。您根本不应该明确设置WidthHeight。在上面的代码中,您似乎将DockPanel 的大小强制为与Window 大小不同的大小,所以它当然不会得到正确的大小。请更准确地解释你要做什么代码,为什么你认为你应该这样做,以及代码做什么。根本不清楚你到底在问什么。

标签: c# wpf xaml wpf-controls


【解决方案1】:

当您使用停靠面板时,您不需要明确设置它。如果你愿意,你可以通过使用最小高度和宽度来进一步限制,但不是强制性的。

    <Window x:Class="WpfApplication2.DMMainWindow"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             ResizeMode="NoResize"
             WindowState="Maximized" 
             WindowStyle="None"
             WindowStartupLocation="CenterScreen"
             Height="{Binding SystemParameters.PrimaryScreenHeight}"
             Width="{Binding SystemParameters.PrimaryScreenWidth}">
             <DockPanel x:Name="mainPanel" 
                        MinHeight ="{Binding SystemParameters.PrimaryScreenHeight}" 
                        MinWidth ="{Binding SystemParameters.PrimaryScreenWidth}"  />

 </Window>

【讨论】:

    【解决方案2】:

    您可以如下添加 Stretch 属性,

    <DockPanel HorizontalAlignment="Stretch"  VerticalAlignment="Stretch"  Margin="0,0,0,0" x:Name="mainPanel" />
    

    希望能成功

    【讨论】:

    • 看起来“Arun D”有助于“arun d”?你的另一个自我将永远帮助你。
    • 我抓不到你 macieqqq
    【解决方案3】:
    <Window x:Class="WpfApplication2.DMMainWindow"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             ResizeMode="NoResize"
             WindowState="Maximized" 
             WindowStyle="None"
             WindowStartupLocation="CenterScreen"
             Height="{Binding SystemParameters.PrimaryScreenHeight}"
             Width="{Binding SystemParameters.PrimaryScreenWidth}">
             <DockPanel x:Name="mainPanel" />
     </Window>
    

    【讨论】:

    • 试试这个,它将完全适合您的窗口。
    最近更新 更多