【问题标题】:Positioning controls - WPF定位控件 - WPF
【发布时间】:2018-01-09 13:08:27
【问题描述】:

我正在自学 WPF,遇到了一个令我困惑的问题。在下面的 XAML 中,当我打开应用程序时,标签和图像位于窗口的左上角,但是当我最大化窗口时,它们会移向中间。我缺少什么会使控件保持其相对位置?

<Window x:Class="WafLuckyDog.Presentation.Views.ShellWindow"
        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:vm="clr-namespace:WafLuckyDog.Applications.ViewModels"
        mc:Ignorable="d" Title="{Binding Title}" Icon="{StaticResource ApplicationIcon}" Width="994.273" Height="840"
        d:DataContext="{d:DesignInstance vm:ShellViewModel}">

    <Window.Background>
        <ImageBrush ImageSource="{StaticResource Background}"></ImageBrush>
    </Window.Background>
    <DockPanel Margin="0,0,-539,0">

    <Grid>
        <Image  Name="Logo" HorizontalAlignment="Left" Margin="0,10,0,669" Width="129" 
                Source="{StaticResource Logo}" />
        <Label Content="Label" HorizontalAlignment="Left" Margin="10,145,0,0" VerticalAlignment="Top" Foreground="AntiqueWhite"/>
    </Grid>
   </DockPanel>
</Window>

【问题讨论】:

  • Margin="0,10,0,669" 左边距为 669px。放大窗口时,您会更清楚地看到页边距
  • @DieterB 669 是底部边距,不是以像素为单位,而是以与设备无关的单位。
  • @DieterB 最后是下边距。左上右下

标签: c# wpf xaml


【解决方案1】:

首先,你有一个DockPanel,它没有关闭标签并且什么也不做,它会给出一个编译错误,所以删除它。此外,删除底部边距并添加 HeightVerticalAlignment 属性。 VerticalAlignmentHorizontalAlignment 确保右上角锚点。

<Window x:Class="WafLuckyDog.Presentation.Views.ShellWindow"
        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:vm="clr-namespace:WafLuckyDog.Applications.ViewModels"
        mc:Ignorable="d" Title="{Binding Title}" Icon="{StaticResource ApplicationIcon}" Width="994.273" Height="840"
        d:DataContext="{d:DesignInstance vm:ShellViewModel}">

    <Window.Background>
      <ImageBrush ImageSource="{StaticResource Background}"></ImageBrush>
    </Window.Background>

    <Grid>
      <Image  Name="Logo" HorizontalAlignment="Left" Margin="0,10,0,0" VerticalAlignment="Top" Width="129" Height="129" 
          Source="{StaticResource Logo}" />
      <Label Content="Label" HorizontalAlignment="Left" Margin="10,145,0,0" VerticalAlignment="Top" Foreground="AntiqueWhite"/>
    </Grid>

</Window>

【讨论】:

  • 那行得通。经过多年的 winforms 和 ASP.net 以及超过我的控制台应用程序。,这将是一个令人兴奋的学习曲线
猜你喜欢
  • 1970-01-01
  • 2011-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-07
相关资源
最近更新 更多