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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:36:43+00:00 2026-05-15T14:36:43+00:00

I have a xaml page with 30 togglebuttons on it and I need to

  • 0

I have a xaml page with 30 togglebuttons on it and I need to bind 4 properties of each togglebutton to a class that I have. I’m able to do the binding, but I’m hoping to find a better solution.

My class currently looks something like this:

public int ToggleButton1Height;
public int ToggleButton1Width;
..
public int ToggleButton2Height;
public int ToggleButton2Width;
..etc

As you can see if I had 4 properties for each togglebutton, that means I need over 120 properties in my class. Is there a better way?

  • 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-15T14:36:44+00:00Added an answer on May 15, 2026 at 2:36 pm

    I’m a little curious about your scenario, but here’s the quickest simples solution I can think of. All the code is explained in the comments, but do let me know if you have questions. Of course there’s a ton more solution on how to do this, but I don’t have a lot of details about what are you trying to do. So I hope this works for you. You should be able to just copy paste all this in a new project and have it working.

    XAML:

    <Window x:Class="StackOverflowTests.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="Window1"
        x:Name="window1"
        Width="800"
        Height="600"
        Loaded="Window1_Loaded">
        <Window.Resources>
            <!-- 
                Create a style so you don't have to define the properties 30 times.
                The style will tell the ToggleButtons which properties in your class they should look at.
                The ToggleButtonClass object will be bound on the code side
            -->
            <Style TargetType="{x:Type ToggleButton}" x:Key="toggleButtonStyle">
                <Setter Property="Height" Value="{Binding ToggleButtonHeight}" />
                <Setter Property="Width" Value="{Binding ToggleButtonWidth}" />
                <Setter Property="Content" Value="{Binding ToggleButtonText}" />
            </Style>
        </Window.Resources>
        <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
            <StackPanel x:Name="theStackPanel">
                <!--
                    You still have to set the style in each button, in case you add buttons that you don't want to use the properties
                    If you don't want to manually set the style, remove the x:Key="toggleButtonStyle" property from the style above
                    and also remove the Style="{StaticResource toggleButtonStyle}" from all the toggle buttons below.
                -->         
                <ToggleButton Style="{StaticResource toggleButtonStyle}" />
                <ToggleButton Style="{StaticResource toggleButtonStyle}" />
                <ToggleButton Style="{StaticResource toggleButtonStyle}" />
                <ToggleButton Style="{StaticResource toggleButtonStyle}" />
                <ToggleButton Style="{StaticResource toggleButtonStyle}" />
                <ToggleButton Style="{StaticResource toggleButtonStyle}" />
                <ToggleButton Style="{StaticResource toggleButtonStyle}" />
                <ToggleButton Style="{StaticResource toggleButtonStyle}" />
                <ToggleButton Style="{StaticResource toggleButtonStyle}" />
                <ToggleButton Style="{StaticResource toggleButtonStyle}" />
                <ToggleButton Style="{StaticResource toggleButtonStyle}" />
                <ToggleButton Style="{StaticResource toggleButtonStyle}" />
                <ToggleButton Style="{StaticResource toggleButtonStyle}" />
                <ToggleButton Style="{StaticResource toggleButtonStyle}" />
                <ToggleButton Style="{StaticResource toggleButtonStyle}" />
                <ToggleButton Style="{StaticResource toggleButtonStyle}" />
                <ToggleButton Style="{StaticResource toggleButtonStyle}" />
                <ToggleButton Style="{StaticResource toggleButtonStyle}" />
                <ToggleButton Style="{StaticResource toggleButtonStyle}" />
                <ToggleButton Style="{StaticResource toggleButtonStyle}" />
                <ToggleButton Style="{StaticResource toggleButtonStyle}" />
                <ToggleButton Style="{StaticResource toggleButtonStyle}" />
                <ToggleButton Style="{StaticResource toggleButtonStyle}" />
                <ToggleButton Style="{StaticResource toggleButtonStyle}" />
                <ToggleButton Style="{StaticResource toggleButtonStyle}" />
                <ToggleButton Style="{StaticResource toggleButtonStyle}" />
                <ToggleButton Style="{StaticResource toggleButtonStyle}" />
                <ToggleButton Style="{StaticResource toggleButtonStyle}" />
                <ToggleButton Style="{StaticResource toggleButtonStyle}" />
                <ToggleButton Style="{StaticResource toggleButtonStyle}" />
            </StackPanel>
        </ScrollViewer>
    </Window>
    

    C#:

    using System.Windows;
    using System.Windows.Controls.Primitives;
    
    namespace StackOverflowTests
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
            }
    
            // Creating all the classes and setting the DataContext of each ToggleButton
            private void Window1_Loaded(object sender, RoutedEventArgs e)
            {
                int sizeFactor = 0;
    
                // for each ToggleButton in the StackPanel, create one instance of the ToggleButtonClass
                // and assign it to the DataContext of the ToggleButton, so all the binding in the Style
                // created in XAML can kick in.
                foreach (UIElement element in theStackPanel.Children)
                {
                    if (element is ToggleButton)
                    {
                        sizeFactor++;
    
                        ToggleButtonClass toggleButtonClass = new ToggleButtonClass()
                        {
                            ToggleButtonHeight = sizeFactor * 20,
                            ToggleButtonWidth = sizeFactor * 50,
                            ToggleButtonText = "Button " + sizeFactor
                        };
    
                        (element as ToggleButton).DataContext = toggleButtonClass;
                    }
                }
            }
        }
    
        // your toggle button class
        public class ToggleButtonClass
        {
            public double ToggleButtonHeight { get; set; }
            public double ToggleButtonWidth { get; set; }
            public string ToggleButtonText { get; set; }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .xaml file and a .cs file that share value with Binding.
I have a xaml page that has an ItemsControl control on it. ItemsControl is
I have a xaml page but transition effect doesn't work, the page is shown
I have a XAML page in a separate Windows Phone class library. The library
Say you have a xaml page that takes two inputs such as DATE and
i have ListBox on my xaml page called MainListBox. i can get index that
I have a two pages say Main.xaml and Details.xaml .Each page has a ListBox
In Silverlight, I have a xaml page that contains a resource dictionary defining many
I have a xaml page that I want to host another xaml page for
I have added a new XAML page to my WP7 app and I need

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.