简体   繁体   中英

C# WPF - How to acces controls added in code

How to change properties of a control which was added in code (Window_Loaded Event). Each one has unique name generated during creation. I want to change IsChecked property for specific CheckBoxes.

Here is a code which I use to add those CheckBoxes:

for (int x = 0; x < 8; x++)
        {
            StackPanel sp = new StackPanel();
            sp.Name = "StackPanel_" + x.ToString();
            sp.Orientation = Orientation.Horizontal;

            for (int y=0; y<8; y++)
            {

                CheckBox rb = new CheckBox();

                rb.Name = "rb_" + x.ToString() + "_" + y.ToString();
                rb.Margin = new Thickness(10, 0, 10, 0);
                rb.IsEnabled = false;
                rb.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                sp.Children.Add(rb);

            }

            Grid.SetRow(sp, x);
            Grid.SetColumn(sp, 1);
            GridBits.Children.Add(sp);
        }

Based on my understanding, you want a 8x8 cell with each cell contains a checkbox to represent 1 bit. You can maintain a 8x8 Array:

CheckBox[,] checkBoxes = new CheckBox[8,8];

Then add the Checkbox with x and y in your code into this list

boxes[x,y] = rb;

According to the bits received, you can relocate it easily with x,y coordinate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM