Title: How to find control created by codebehind using name in WPF
Tags: c#;asp.net;wpf;xaml;findname
Question: I am currently attempting to create a MIPS emulator using WPF.
In a stack panel control called registerList; I created 32 stack panels (named C0 to C31) that each contain 2 textblocks (second textblock named R0 to R31).
I initially display the 32 registers contained on Procesor.RegisterBank.Registers[i] correctly using the displayRegisters() method I created.
I made a method to update all 32 values of second textblocks with the values contained on a class called Procesor.RegisterBank.
However when attempting to use this method, the GUI page will not display when I start the project.
I've been stuck on this for hours and almost considering not creating the stack panels via code.
Thanks
public partial class MainWindow : Window
MRA.MRA Procesor = new MRA.MRA();
public MainWindow()
InitializeComponent();
displayRegisters();
Procesor.RegisterBank.registers[1] = 69;
updateRegisters();
public void updateRegisters()
for (int i = 0; i < Procesor.RegisterBank.registers.Length; i++)
{
var register = (StackPanel)registerList.FindName("C"+i.ToString());
var content = (TextBlock)register.FindName("R"+i.ToString());
content.Text= Procesor.RegisterBank.registers[i].ToString();
}
public void displayRegisters()
for (int i = 0; i < Procesor.RegisterBank.registers.Length; i++)
{
var register = new StackPanel { Orientation = Orientation.Horizontal, Background = Brushes.White, Margin = new Thickness(6, 6, 6, 6), MinHeight = 25, Name = "C" + i.ToString() };
var registerName = new TextBlock { Text = "R"+i.ToString(), FontSize = 16, MinWidth = 30, Background = Brushes.DarkGray, Foreground = Brushes.White};
var registerValue = new TextBlock { Text = Procesor.RegisterBank.registers[i].ToString(), FontSize = 16, MinWidth = 100, HorizontalAlignment = HorizontalAlignment.Right, Name="R"+i.ToString()};
register.Children.Add(registerName);
register.Children.Add(registerValue);
registerList.Children.Add(register);
}
Comment: "almost considering not creating the stack panels via code."  Almost. You almost considered doing it right, but that would be too easy. I suggest you either use winforms, or use WPF properly. Trying to use WPF as winforms ends in anguish.

Here is the accepted answer: You have to use RegisterName() , so that newly added Control can be part of the NameScope of parent control.
Add these lines at the end in your displayRegister() method :
NameScope.SetNameScope(register, new NameScope());
register.RegisterName(registerValue.Name, registerValue);
registerList.RegisterName(register.Name, register);


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:09/25/16', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><asp.net><wpf><xaml><findname>', 'Title': 'How to find control created by codebehind using name in WPF', 'ViewCount': 551, 'id': 39682350, 'user_AboutMe': None, 'user_AccountId': 2846120.0, 'user_DisplayName': 'Carlos Perez', 'user_Reputation': 13.0, 'user_Views': 26.0, 'user_id': 2444467.0}}