I start with:
C#
I want to place the word "Discarded" on the button. I tried adding
C#
DiscardedNo matter where I insert
I also want a white Foreground.
I start with:
I want to place the word "Discarded" on the button. I tried adding
DiscardedNo matter where I insert
I also want a white Foreground.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Naimish MakwanaRank #324 · 7.7K reputationPosted May 20, 2024, 9:41 AM EDT
The issue you’re facing is due to the fact that a
Button.Contentcan only contain one child element. In your case, you’re trying to add both aRectangleand aTextBlockas direct children ofButton.Content, which is not allowed.To solve this, you can use a
Gridor aStackPanelto contain both theRectangleand theTextBlock. Here’s how you can do it:In this code, I’ve created a custom
ControlTemplatefor theButton. Inside this template, I’ve added aGridthat contains both theRectangleand theTextBlock. TheTextBlockis centered horizontally and vertically within theGrid. TheForegroundproperty of theTextBlockis set to “White”, which will make the text appear white. TheButton’sWidthandHeightare set to “90” and “30” respectively to match the size of your originalRectangle. This should give you a rectangular button with the word “Discarded” on it. The word will be white and centered on the button.Please replace the
#444444with the actual color code you want to use for the button background. If you want to keep the gray color, you can use#444444.Thanks