【问题标题】:WPF Program Issue in XAML not displaying Buttons properlyXAML 中的 WPF 程序问题未正确显示按钮
【发布时间】:2016-06-11 00:52:47
【问题描述】:

我在将 WPF 程序从“Visual Studio Express 2013 for Windows Desktop”移动到“Visual Studio Community 2015”时遇到问题。

我最初的 WPF 程序是在“VS Express 2013 for Windows Desktop”中创建的。它依赖于能够将 Grid 嵌入到 Button 中。在那个网格上(在按钮内),我会放置矩形。它在 VS Express 2013 中运行良好,但在 VS Community 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>

VS Express 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>

里面有白色十字的黑色按钮在哪里?什么改变了我的原始代码?

【问题讨论】:

    标签: wpf visual-studio xaml


    【解决方案1】:

    您的按钮宽度和高度设置为 0。更改值以满足您的需要或选择不带按钮的方法。

    【讨论】:

    • 是的,我知道高度和宽度设置为 0。它在 VS Express 2013 中运行良好。我编写了一个庞大的程序,它依赖于以这种方式处理按钮。我不知道为什么在较新版本的 VS 和 WPF 中行为发生了变化。
    • 变化没有跳出。
    【解决方案2】:

    我将回答我自己的问题。 我找到了问题的解决方案。 显然旧版本的 WPF 使用以下代码

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

    旧版本将这些“0”设置视为“自动”。 但是现在你必须专门使用“auto”或“Auto”这个词......“0”将不再给出想要的结果。

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

    以上都解决了这个问题。

    【讨论】:

      猜你喜欢
      • 2011-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-07
      • 1970-01-01
      相关资源
      最近更新 更多