简体   繁体   中英

Reference a user control by name in code behind (dynamically created)

I created a checkbox using the method below:

for( i = 1; i<7; i++)
    {
        for (j = 1; j < 33; j++)
        {
            CheckBox a = new CheckBox();
            a.Name = "SAT_ID_" + i.ToString() + "_" + j.ToString();
            this.Sat_ID_Grid.Children.Add(a);
            a.Style = (Style)Application.Current.FindResource("ReadOnlyCheckBox");
            Grid.SetRow(a, i );
            Grid.SetColumn(a, j );
        }
    }

Is it possible to reference the checkboxes later using "SAT_ID_X_Y"? I cant seem to find the solution. If not how can I reference them? I need to change the .ischecked state.

Thanks

Just store the references to the controls somewhere instead of their names, eg in a List<CheckBox> , then you can access them by index.

Also you actually should not do any of that, use data-templating and data-binding to create controls for data. If done right you just need to change a boolean on your data and the check-box will be checked/unchecked.

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