How can I create a button which Rectangle object filled with a color in WPF with C#?

空扰寡人 提交于 2019-12-11 00:24:24

问题


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.



来源:https://stackoverflow.com/questions/1756918/how-can-i-create-a-button-which-rectangle-object-filled-with-a-color-in-wpf-with

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!