Click here to Skip to main content
15,893,790 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to access textbox and label controls for other c# classes(not only MainWindow class) in wpf.
Is it possible to populate MainWindow class Tools for other classes? 
Here is my simplified example code:


XML
<Window x:Class="MyAbsClass.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MyAbsClass"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBox x:FieldModifier="public" x:Name="txt1"/>
        <TextBox x:FieldModifier="public" x:Name="txt2"/>
        <Button x:FieldModifier="public" x:Name="btn1" Content="Button" Click="btn1_Click"/>
    </Grid>
</Window>


C#
namespace MyAbsClass
{   
    class manipulate
    {
        public void add()
        {
           int a=int32.Parse(txt1.Text);
	   int b=int32.Parse(txt2.Text);
        }
    }   
    
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();            
        }

        private void btn1_Click(object sender, RoutedEventArgs e)
        {
		MessageBox.Show("Added value= "+(a+b));
        }
    }
}


What I have tried:

I used x:FieldModifier="public" but still negative.
Posted
Updated 10-Jun-16 7:47am
Comments
Philippe Mori 10-Jun-16 8:48am    
You should never access UI controls from outside of the window that contains them. This would lead to unmaintainable code as it would tie the UI to the business code which is really a bad thing.

The code you show won't work: it won't even compile!
C#
MessageBox.Show("Added value= "+(a+b));

The only "a" and "b" you show are in a different class, and are local to the add method, which means that they are available only within that method. Even if they were part of the class (i.e. public or private) they wouldn't be directly accessible as part of the main window as they would be instance based and would require an example of the manipulate class to work.
And the manipulate class can't access the text boxes because it's not a control at all and can't host UI elements.
The text boxes are part of the MainWindow class, and are only supposed to be accessible within it.
If the manipulate class needs to access the values, they must be passed from the window to the appropriate instance of the class, either via a constructor, or via properties:
C#
class Manipulate
    {
    public string ParamA { get; set; }
    public string ParamB { get; set; }
    public int Add()
       {
       int a=int32.Parse(ParamA);
       int b=int32.Parse(ParamB);
       return a + b;
       }
    }

C#
public partial class MainWindow : Window
    {
    public MainWindow()
       {
       InitializeComponent();
       }
     private void btn1_Click(object sender, RoutedEventArgs e)
       {
       Manipulate m = new Manipulate();
       m.ParamA = txt1.Text;
       m.ParamB = txt2.Text;
       MessageBox.Show("Added value= " + m.Add());
       }
    }
 
Share this answer
 
v3
First thing to understand: most usually, you do something not "from class", but "from a class instance", unless the "other class" method is static. Likewise, and more importantly, the "form" means the instance of some class derived from System.Windows.Forms.Form. Normally, the controls you mentioned in the questions, are the instance members of some Form class; therefore, you need an instance, a reference to the form instance, to read or modify the state of the control or any control's properties.

It does not matter how you pass the instance. For example, you can have "other class" with constructor which accept a parameter of specific form class. In a form class, you can expose a control, so you would have its reference through the form reference:
C#
myMainForm.myLabel.Text = "Done";

However, this is not a clean or neat way of doing things. It's really bad to expose controls directly. If some class has reference to some form controls, this class should better be the same form (one good idea: the same partial declaration in a separate file, but a part of the same class). It's much better to expose some functionality. The class working with the form does not have to "know" this is the form. It's better be some interface instance exposing only the interface members required to implement some functionality. The form class implements some interface. The idea is explained in my article: Many Questions Answered at Once — Collaboration between Windows Forms or WPF Windows.

Don't be confused: the article explain collaboration of a form with a form or a WPF window with a window, just because this is one of the frequently asked questions (very frequently :-)). But nothing changes if this is the collaboration between a form and some object of a non-form class.

—SA
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900