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 6049469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:34:50+00:00 2026-05-23T07:34:50+00:00

I found myself needing to define some styles and templates for a couple ToggleButton

  • 0

I found myself needing to define some styles and templates for a couple ToggleButton elements that I’m using in one of my programs. I have everything working, except that whatever I set as the content on them doesn’t show. I’m at a loss as to how to get my contents to display correctly. My code is as follows:

    private Style m_ToggleStyle;
    private Style ToggleStyle
    {
        get
        {
            if (m_ToggleStyle == null)
            {
                lock (new object())
                {
                    if (m_ToggleStyle == null)
                    {
                        m_ToggleStyle = new Style(typeof(ToggleButton));

                        m_ToggleStyle.Setters.Add(new Setter { Property = ToggleButton.TemplateProperty, Value = ToggleTemplate });
                    }
                }
            }
            return m_ToggleStyle;
        }
    }

    private ControlTemplate m_ToggleTemplate;
    private ControlTemplate ToggleTemplate
    {
        get
        {
            if (m_ToggleTemplate == null)
            {
                lock (new object())
                {
                    if (m_ToggleTemplate == null)
                    {
                        m_ToggleTemplate = new ControlTemplate();
                        FrameworkElementFactory g = new FrameworkElementFactory(typeof(Grid));
                        g.SetValue(Grid.MarginProperty, new Thickness(0));
                        g.Name = "MainGrid";

                        FrameworkElementFactory cp = new FrameworkElementFactory(typeof(ContentPresenter));
                        cp.SetValue(ContentPresenter.NameProperty, "Contents");
                        cp.SetValue(ContentPresenter.HorizontalAlignmentProperty, System.Windows.HorizontalAlignment.Stretch);
                        cp.SetValue(ContentPresenter.VerticalAlignmentProperty, System.Windows.VerticalAlignment.Stretch);
                        cp.SetValue(ContentPresenter.MarginProperty, new Thickness(2));
                        cp.Name = "Contents";

                        g.AppendChild(cp);
                        m_ToggleTemplate.VisualTree = g;

                        ImageBrush ibBackgroundUnselected = new ImageBrush();
                        ibBackgroundUnselected.ImageSource = new BitmapImage(new Uri("pack://application:,,,/Controls;component/Resources/TabUnselected.png", UriKind.Absolute));
                        g.SetValue(Grid.BackgroundProperty, ibBackgroundUnselected);

                        ImageBrush ibBackgroundSelected = new ImageBrush();
                        ibBackgroundSelected.ImageSource = new BitmapImage(new Uri("pack://application:,,,/Controls;component/Resources/TabSelected.png", UriKind.Absolute));

                        Trigger tIsChecked = new Trigger { Property = ToggleButton.IsCheckedProperty, Value = true };
                        tIsChecked.Setters.Add(new Setter(Grid.BackgroundProperty, ibBackgroundSelected, "MainGrid"));

                        m_ToggleTemplate.Triggers.Add(tIsChecked);

                    }
                }
            }
            return m_ToggleTemplate;
        }
    }

This is used as I dynamically create ToggleButtons like:

ToggleButton tbExample = new ToggleButton();
tbExample.Style = ToggleStyle;
tbExample.Content = "Content";
  • 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-05-23T07:34:50+00:00Added an answer on May 23, 2026 at 7:34 am

    Found the problem:

    m_ToggleTemplate = new ControlTemplate();
    

    Should have been:

    m_ToggleTemplate = new ControlTemplate(typeof(ToggleButton));
    

    Apparently if you don’t set the type of the ControlTemplate, it only partially works.

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

Sidebar

Related Questions

Recently, I found myself having to write up some concerns I have about race
I'm currently working on project with Haskell, and have found myself some trouble. I'm
I'm teaching myself some basic scraping and I've found that sometimes the URL's that
I'm studying some code examples that I haven't written myself, and I found this
I just found myself creating a class called InstructionBuilderFactoryMapFactory. That's 4 pattern suffixes on
While fleshing out a hypothetical domain model, I have found myself wondering whether the
Found some old code, circa VS 2003. Now I have just VS 2008 (SP1)
I'm trying to make myself a GUI library using plain WinAPI. But I found
I've found myself increasingly unsatisfied with the DataSet/DataTable/DataRow paradigm in .Net, mostly because it's
Lately I've found myself constantly running ILDASM to study the MSIL output of my

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.