How can I create a button which Rectangle object f

2019-07-13 11:37发布

问题:

How can I create Button control which contains a Rectangle object filled with the color represented by the Colors.Aqua?

I have a rectangle

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

and I have a button:

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

I can't figure out how to combine these things.

Any ideas?

回答1:

button.Content = rectangle;

or in XAML that would be like

<Button>
  <Button.Content>
     <Rectangle Width="100" Height="50">
        <Rectangle.Fill>
           <SolidColorBrush Color="Aqua" />
         </Rectangle.Fill>
      </Rectangle>
  </Button.Content>
</Button>


回答2:

You can also define a Style in your XAML, set the TargetType to Button, and then set the Background property's Value to "Aqua". Of course, you'll also have to specify the Style for all of the Buttons that you drop into your interface so that they get the desired look.