代码之家  ›  专栏  ›  技术社区  ›  sproketboy

如何使用C#在WPF中创建一个用颜色填充矩形对象的按钮?

  •  2
  • sproketboy  · 技术社区  · 16 年前

    Rectangle rectangle = new Rectangle();
    rectangle.Fill = new SolidColorBrush(Colors.Aqua);
    rectangle.Width = 100;
    rectangle.Height = 50;
    

    Button button = new Button();
    button.Content = "Button";
    

    2 回复  |  直到 16 年前
        1
  •  4
  •   Massimiliano    16 年前
    button.Content = rectangle;
    

    <Button>
      <Button.Content>
         <Rectangle Width="100" Height="50">
            <Rectangle.Fill>
               <SolidColorBrush Color="Aqua" />
             </Rectangle.Fill>
          </Rectangle>
      </Button.Content>
    </Button>
    
        2
  •  2
  •   Dave    16 年前

    您还可以在XAML中定义样式,将TargetType设置为Button,然后将Background属性的值设置为“Aqua”。当然,您还必须为放入界面的所有按钮指定样式,以便它们获得所需的外观。