2016-02-27 65 views
0

我在将WPF程序从“Visual Studio Express 2013 for Windows Desktop”移动到“Visual Studio Community 2015”时遇到问题。XAML中的WPF程序问题无法正确显示按钮

我的原始WPF程序创建于“VS Express 2013 for Windows Desktop”。它依赖于能够将网格嵌入按钮。在该网格上(按钮内),我会放置矩形形状。它在VS Express 2013中正常工作,但在VS社区2015中无法正常工作。

下面的示例创建于VS Express 2013。它正确地显示了一个红色边框矩形和一个带白色十字的黑色按钮。

<Window x:Class="TestErrors.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="200" Width="200"> 
    <Canvas> 
     <Rectangle 
     Stroke="Red" 
     StrokeThickness="10" 
     Width="50" 
     Height="50" 
     Canvas.Left="75" 
     Canvas.Top="20"/> 
     <Button 
      Width="0" 
      Height="0" 
      Canvas.Left="100" 
      Canvas.Top="100"> 
      <Canvas> 
       <Rectangle 
       Fill="Black" 
       Width="30" 
       Height="30" 
       Canvas.Left="-15" 
       Canvas.Top="-15"/> 
       <Rectangle 
       Fill="White" 
       Width="4" 
       Height="20" 
       Canvas.Left="-2" 
       Canvas.Top="-10"/> 
       <Rectangle 
       Fill="White" 
       Width="20" 
       Height="4" 
       Canvas.Left="-10" 
       Canvas.Top="-2"/> 
      </Canvas> 
     </Button> 
    </Canvas> 
</Window> 

enter image description here

VS表达上述图像2013是正确的......白十字的黑色按钮正确显示。

以下示例中的Visual Studio Community 2015中的相同XAML仅显示红色矩形。它没有按我的预期工作。

<Window x:Class="TestErrors.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:TestErrors" 
    mc:Ignorable="d" 
    Title="MainWindow" Height="200" Width="200"> 
    <Canvas> 
     <Rectangle 
     Stroke="Red" 
     StrokeThickness="10" 
     Width="50" 
     Height="50" 
     Canvas.Left="75" 
     Canvas.Top="20"/> 
     <Button 
      Width="0" 
      Height="0" 
      Canvas.Left="100" 
      Canvas.Top="100"> 
      <Canvas> 
       <Rectangle 
       Fill="Black" 
       Width="30" 
       Height="30" 
       Canvas.Left="-15" 
       Canvas.Top="-15"/> 
       <Rectangle 
       Fill="White" 
       Width="4" 
       Height="20" 
       Canvas.Left="-2" 
       Canvas.Top="-10"/> 
       <Rectangle 
       Fill="White" 
       Width="20" 
       Height="4" 
       Canvas.Left="-10" 
       Canvas.Top="-2"/> 
      </Canvas> 
     </Button> 
    </Canvas> 
</Window> 

enter image description here

凡与白十字内的黑色按钮?什么改变了我的原始代码?

回答

1

您的按钮宽度和高度设置为0.更改值以符合您的需求或选择无按钮的方法。

+0

是的,我知道高度和宽度设置为0.并且它在VS Express 2013中工作正常。我有一个编写的巨大程序,它依靠这种方式处理按钮。我不知道为什么VS和WPF的新版本中的行为发生了变化。 – user3130331

+0

所有更改都记录在这里:https://msdn.microsoft.com/en-us/library/hh367887(v=vs.110).aspx –

+0

谢谢彼得...我会看看... – user3130331

0

我要回答我自己的问题。 我找到了解决问题的办法。 显然,旧版本的WPF与下面的代码

<Button 
     Width="0" 
     Height="0" 

认为这些“0”设置为“自动”的旧版本的工作。 但是现在您必须专门使用“自动”或“自动”这个词。“0”不会再给出想要的结果。

<Button 
     Width="Auto" 
     Height="Auto" 

<Button 
     Width="auto" 
     Height="auto" 

以上两个都解决了这个问题。