Hello, I add a listbox to the wpf form. Here is a fix:

<ListBox Height="311" HorizontalAlignment="Left" Name="listBox1" VerticalAlignment="Top" Width="503" /> 

Here is how I address:

 listBox1.Item.Add("3333"); 

When I try to access the component, the compiler swears:

 The name 'listBox1' does not exist in the current context 

How to solve it? Listbox is in grid.

    1 answer 1

    Do you call InitializeComponent() method in the form constructor? In the xaml file, the window should have an x:Class property pointing to your class. ListBox1 will be available only from the window class, you may declare a ListBox in one and use it in another.

    • Yes, it was this, but how now to call one component from several classes? - Petrovich
    • Using one element in different classes is not very good, but if necessary, then you can make a public property that will return this element. Well, or just pass the link to the ListBox in the desired function, which is also not good. - sys_27