简体   繁体   English

Visual Studio 2010 XAML布局编辑器不提供“所见即所得”的功能

[英]Visual Studio 2010 XAML layout editor not WYSIWYG

When I'm creating WPF layouts in Visual Studio 2010 I just drag and drop controls from the Toolbox onto the default element, typically a Grid. 在Visual Studio 2010中创建WPF布局时,只需将控件从“工具箱”拖放到默认元素(通常是网格)上。 When I do this the spacing shown in Visual studio does not match what's displayed at runtime. 当我这样做时,Visual Studio中显示的间距与运行时显示的间距不匹配。 For example, here are the visual editor and runtime results, respectively, for two layouts . 例如,以下分别是两个布局的可视化编辑器和运行时结果。 . .

编辑

运行

. . . and . 和。 . .

编辑

运行

... notice the spacing is altered. ...请注意,间距已更改。
Here's the XAML that was generated for the two examples . 这是为两个示例生成的XAML。 . .

  <Button Content="ProjectPattern" Height="23" HorizontalAlignment="Left" Margin="12,296,0,0" Name="butProjPattern" VerticalAlignment="Top" Width="117" Click="butProjPattern_Click" />
    <TextBlock Height="22" HorizontalAlignment="Left" Margin="135,0,0,141" Name="ResultProjPattern" Text="(result)"       VerticalAlignment="Bottom" Width="41" />
    <TextBlock Height="23" HorizontalAlignment="Left" Margin="175,295,0,0" Name="TextBlockPattern" Text="     (pattern file)" Padding="4" VerticalAlignment="Top" Width="135" Background="#FFF1F3FF" /> 
    <Button Content="...browse" Height="23" HorizontalAlignment="Right" Margin="0,294,814,0" Name="buttonPatternBrowse" VerticalAlignment="Top" Width="54" Click="buttonPatternBrowse_Click" />

...and... ...和...

<Button Content="Display Cross" Height="24" HorizontalAlignment="Left" Margin="622,108,0,0" Name="buttonCross" VerticalAlignment="Top" Width="117" Click="buttonCross_Click" />
<TextBlock Height="24" HorizontalAlignment="Right" Margin="0,107,344,0" Name="ResultCross" Text="(result)" VerticalAlignment="Top" Width="100" />
<Button Content="Display Diamond" Height="23" HorizontalAlignment="Left" Margin="622,138,0,0" Name="butDiamond" VerticalAlignment="Top" Width="117" Click="butDiamond_Click" />
<TextBlock Height="23" HorizontalAlignment="Right" Margin="0,138,344,0" Name="ResultDiamond" Text="(result)" VerticalAlignment="Top" Width="100" />
<Button Content="DisplayFullField" Height="24" HorizontalAlignment="Left" Margin="622,165,0,0" Name="butFullField" VerticalAlignment="Top" Width="117" />
<TextBlock Height="24" HorizontalAlignment="Right" Margin="0,164,344,0" Name="ResultFullField" Text="(result)" VerticalAlignment="Top" Width="100" />

I added button handlers, text content, etc, but the layout is strictly what was generated by the Visual Studio layout editor. 我添加了按钮处理程序,文本内容等,但是布局严格来说是Visual Studio布局编辑器生成的。 One thing I noticed is that even though it's drag-and-drop from the Toolbox onto the Grid, Visual Studio is inconsistent about whether it chooses "Left" or "Right" for its HorizontalAlignment for different elements. 我注意到的一件事是,即使将它从“工具箱”拖放到网格上,Visual Studio对于为不同元素的Horizo​​ntalAlignment选择“左”还是“右”也不一致。

The container element is just a default Grid in a default Window - it can minimize but not resize . 容器元素只是默认窗口中的默认网格-它可以最小化但不能调整大小。 . .

<Window x:Class="Caller1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="500" Width="1200" ResizeMode="CanMinimize">
    <Grid Height="460"> 

The result is that I have to waste lots of time going between the editor and running my app to "tweak" my layout. 结果是我不得不浪费大量时间在编辑器和运行应用程序之间来“调整”布局。 What prevents the "WYSIWYG" layout editor from matching the runtime layout and how can I get them to correspond? 是什么阻止“ WYSIWYG”布局编辑器与运行时布局匹配,如何使它们对应?

Thanks in advance. 提前致谢。

I can't see your images (dumb work firewall), but I can tell by your XAML that you are using the Grid wrong. 我看不到您的图像(哑工作防火墙),但是通过您的XAML可以告诉您您使用的是Grid错误。 I understand that this may be the fault of the editor, but seriously, you shouldn't be setting the Width and Height properties on all your elements. 我知道这可能是编辑器的错,但是,严重的是,您不应该在所有元素上设置WidthHeight属性。

If you want a fixed size for the Grid 's children, set up some RowDefinition s and ColumnDefinition s and set those sizes. 如果要为Grid的子级设置固定大小,请设置一些RowDefinitionColumnDefinition并设置这些大小。 Your layout is much easier to work with (and will probably be more accurate in the designer) if you do this. 如果这样做,您的布局将更易于使用(并且在设计器中可能会更加准确)。 So, your first Grid would look like this: 因此,您的第一个Grid如下所示:

<Grid Margin="12,0,0,0">  
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="117" />
    <ColumnDefinition Width="Auto" />
    <ColumnDefinition Width="138" /> <!-- This is your desired width + horizontal margins -->
    <ColumnDefinition Width="Auto" />
  </Grid.ColumnDefinitions>
  <Button x:Name="butProjPattern" 
          Grid.Column="0" 
          Content="ProjectPattern" 
          VerticalAlignment="Center" 
          Click="butProjPattern_Click" />
  <TextBlock x:Name="ResultProjPattern" 
             Grid.Column="1" 
             Margin="6,0,0,0" 
             Text="(result)" 
             VerticalAlignment="Center" />
  <TextBlock x:Name="TextBlockPattern" 
             Grid.Column="2"
             Margin="3,0,0,0" 
             Padding="4"
             Text="     (pattern file)" 
             Background="#FFF1F3FF" /> 
  <Button x:Name="buttonPatternBrowse" 
          Grid.Column="3"
          Content="...browse" 
          Margin="22,0,0,0" 
          VerticalAlignment="Center" 
          Padding="2,1"
          Click="buttonPatternBrowse_Click"  />
</Grid>

The other thing I want to note is that after I put this in Kaxaml , which is great for rapid prototyping of your XAML layouts, I noticed that you should really just be using a StackPanel . 我还要注意的另一件事是,在将其放入Kaxaml (非常适合快速构建XAML布局的原型)之后,我注意到您实际上应该只使用StackPanel The first grid could really be this much simpler implementation (you will have to set the Width on a couple of your items to match the layout): 第一个网格实际上可能是这样简单得多的实现(您必须在几个项目上设置Width以匹配布局):

<StackPanel Margin="12,0,0,0" Orientation="Horizontal">  
  <Button x:Name="butProjPattern" 
          Content="ProjectPattern" 
          Width="117"
          Click="butProjPattern_Click" />
  <TextBlock x:Name="ResultProjPattern" 
             Margin="6,0,0,0" 
             Text="(result)" 
             VerticalAlignment="Center" />
  <TextBlock x:Name="TextBlockPattern" 
             Margin="3,0,0,0" 
             Padding="4"
             Width="135"
             Text="     (pattern file)" 
             Background="#FFF1F3FF" /> 
  <Button x:Name="buttonPatternBrowse" 
          Content="...browse" 
          Margin="22,0,0,0" 
          VerticalAlignment="Center" 
          Padding="2,1"
          Click="buttonPatternBrowse_Click"  />
</StackPanel>

This answer is not directly an answer to your actual question, but instead a suggestion to avoid your problem. 此答案不是您实际问题的直接答案,而是避免出现问题的建议。

You will often get UI control placement problems when you use exact control positioning with the Margin property. 当对Margin属性使用精确的控件定位时,通常会遇到UI控件放置问题。 It is a good procedure (and in WPF especially) to allow controls to place and/or space themselves using the various Alignment options available. 这是一个很好的过程(尤其是在WPF中),允许控件使用可用的各种“ Alignment选项自行放置和/或Alignment I have always managed to get the desired layout using this method. 我一直设法使用这种方法来获得所需的布局。

For example, had you used a Grid or StackPanel to arrange your controls, you would not have come across this problem. 例如,如果使用GridStackPanel布置控件,则不会遇到此问题。 Many WPF developers that I know don't even bother using the designer in Visual Studio because of the problems that you mentioned... after dragging and dropping a control and then going to the XAML to remove unwanted values, it often works out quicker to simply type the XAML myself. 我所知道的许多WPF开发人员甚至都不用在Visual Studio中使用设计器,因为您提到的问题...在拖放控件然后转到XAML删除不需要的值后,它通常可以更快地解决只需自己输入XAML。

If you really want to know why the Visual Studio designer... well, er... doesn't work properly, you may have more luck asking on the Microsoft Developer Forum , where you question might actually get seen by a Microsoft employee. 如果您真的想知道为什么Visual Studio设计器...好吧,呃...不能正常工作,那么您可能会Microsoft Developer Forum上遇到更多的运气,您在哪里实际上可能会被Microsoft员工看到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM