Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8583495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:33:08+00:00 2026-06-11T21:33:08+00:00

I’m new to WPF so this may be a stupid question but here it

  • 0

I’m new to WPF so this may be a stupid question but here it is.
I’m trying to create custom button using the XAML thing, so this is what I have:

<Button x:Class="Controls.ShinyButton"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" Height="40" Width="150">
    <Button.Resources>
        <ResourceDictionary Source=".\Resources.xaml" />
    </Button.Resources>

    <Border Background="{StaticResource BlueGradient}" CornerRadius="5">
        <DockPanel>
            <Image x:Name="imgIcon" DockPanel.Dock="Left" Height="32" Margin="4"/>
            <TextBlock DockPanel.Dock="Right" Text ="Test" VerticalAlignment="Center" HorizontalAlignment="Left" Foreground="White" />
        </DockPanel>
    </Border>

    <Button.Background>
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="#FF02B6FF" Offset="0" />
            <GradientStop Color="#FF0084F0" Offset="1" />
        </LinearGradientBrush>
    </Button.Background>
 </Button>

The designer understands it and it compiles without errors, but when I try to run the program and create an instance of it, I get the error:
“Content of a ContentControl must be a single element.”

My resources file is:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <LinearGradientBrush x:Key="BlueGradient">
        <GradientStop Offset="0" Color="#FF02B6FF"/>
        <!-- (2, 182, 255)-->
        <GradientStop Offset="1" Color="#FF0084F0"/>
        <!-- (0, 132, 240) -->
    </LinearGradientBrush>

</ResourceDictionary>

I have the DockPanel inside my Border so the button has only one child…
The “ShinyButton” class looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Controls
{
    /// <summary>
    /// Interaction logic for ShinyButton.xaml
    /// </summary>
    public partial class ShinyButton : Button
    {
        public ShinyButton()
        {
            InitializeComponent();
        }
    }
}

In the main function I add it like this:

public MainWindow()
        {
            InitializeComponent();

            ShinyButton shiny = new ShinyButton();
            this.AddChild(shiny); //This is where the exception is thrown.

        }

What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T21:33:09+00:00Added an answer on June 11, 2026 at 9:33 pm

    Just a guess and atm I am not able to test it, but try following code:

    <Button.Template>
        <ControlTemplate>
            <Border Background="{StaticResource BlueGradient}" CornerRadius="5">
                <DockPanel>
                    <Image x:Name="imgIcon" DockPanel.Dock="Left" Height="32" Margin="4"/>
                    <TextBlock DockPanel.Dock="Right" Text ="Test" VerticalAlignment="Center" HorizontalAlignment="Left" Foreground="White" />
                </DockPanel>
            </Border>
        </ControlTemplate>
    </Button.Template>
    

    EDIT: Ok next try:

    Instead of adding your ShinyButton to the Window (remove this.AddChild(shiny)), try adding your ShinyButton to the Grid by adding this code:

    NameOfGrid.Children.Add(shiny);
    

    The reason of this error is that your Window just can have one child and I bet in your case there is already a Grid as this child.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This could be a duplicate question, but I have no idea what search terms
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.