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

  • Home
  • SEARCH
  • 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 8718247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:29:47+00:00 2026-06-13T06:29:47+00:00

I am working on a WPF where I need to dynamically generate Togglebutton 16

  • 0

I am working on a WPF where I need to dynamically generate Togglebutton 16 times. It will be inefficient if I write down this Togglebutton 16 times and have individual Button Click Command for them.

XAML:

<Togglebutton Height="14" Command="{Binding TogglebuttonGen}" Margin="0" Name="checkBox1" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Center" />

I would ideally want to generate them 16 times and have one common method in my viewmodel class as follows:

private ICommand mTogglebuttonGen;
    public ICommand TogglebuttonGen
    {
        get
        {
            if (mTogglebuttonGen == null)
                mTogglebuttonGen = new DelegateCommand(new Action(mTogglebuttonGenExecuted), new Func<bool>(mTogglebuttonGenCanExecute));

            return mTogglebuttonGen;
        }
        set
        {
            mTogglebuttonGen = value;
        }
    }

    public bool mTogglebuttonGenCanExecute()
    {
        return true;
    }

    public void mTogglebuttonGenExecuted(some INDEX parameter which gives me selected Togglebutton)
    {
        // Have a common method here which performs operation on each Togglebutton click based on INDEX which determines which Togglebutton I have selected
    }

I had done it in my C++ app as follows:

for(int j = 0; j < 16; j ++)
    {
        m_buttonActiveChannels[j] = new ToggleButton();
        addAndMakeVisible(m_buttonActiveChannels[j]);
        m_buttonActiveChannels[j]->addButtonListener(this);
    }

//Checking which Togglebutton is clicked
unsigned bit = 0x8000;
for(int i = 15; i >= 0; i--)
{
    if(0 != (value & bit)) //Value has some hardcoded data
    {
        m_buttonActiveChannels[i]->setToggleState(true);
    } 
    else
    {
        m_buttonActiveChannels[i]->setToggleState(false);
    }

    bit >>= 1;
}

Hence this generates it 16 times and has one method which performs operation based on index i.

How can I achieve it in my wpf app? 🙂

  • 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-13T06:29:48+00:00Added an answer on June 13, 2026 at 6:29 am

    In your viewmodel, create a collection with 16 instances of your ToggleAction class.

    public ObservableCollection<MyToggleActionClass> MyItems {get;set;}
    
    
    public class MyToggleActionClass
    {
       public string DisplayName {get;set;}
       public int Index {get;set;}
    }
    

    Bind this collection to the ItemsSource of a ItemsControl, and that’s it.

    <ItemsControl ItemsSource="{Binding MyItems}">
     <ItemsControl.ItemTemplate>
      <DataTemplate>
        <ToggleButton Content="{Binding Displayname}"
                      Command="{Binding DataContext.TogglebuttonGen, RelativSource={RelativSource AncestorType=ItemsControl}}"
                      CommandParameter="{Binding Index}"/>
      </DataTemplate>
     </ItemsControl.ItemTemplate>
    </ItemsControl>
    

    This is all handwritten, so check for errors. What’s most important is the command binding. If you want to call the command from your ViewModel, you have to use RelativeSource to walk up to the ViewModel DataContext.

    I would also handle/bind the toggle state in MyToggleActionClass.

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

Sidebar

Related Questions

I am working on a WPF where I need to dynamically generate Checkboxes 16
I am working on a WPF application where I need to dynamically generate 2
While working in WPF i have the need for a Dynamic Grid. By this
I have been working on a C# 4.0 WPF project and need to figure
I'm working with WPF and often have the need to get the namespace and
I'm working to build a .NET program with WPF that will need to be
Working in WPF and C#, I have a TransformedBitmap object that I either: Need
I'm working in WPF and i have an interesting requirement. I need my checkboxes
I am working in WPF and I need to set the Text and ToolTip
I am working on a wpf application. Here I need to use System.Windows.Forms.FolderBrowserDialog in

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.