开发者

WPF and C#: Accessing, referencing and setting value to control properties from other class

开发者 https://www.devze.com 2023-03-11 18:49 出处:网络
A really simple setup - Visual Studio 2010\'s default new WPF project, with MainWindow and App classes and XAML. On MainWindow there\'s only a simple control, a texbox, let\'s call it TB.

A really simple setup - Visual Studio 2010's default new WPF project, with MainWindow and App classes and XAML. On MainWindow there's only a simple control, a texbox, let's call it TB. All I'm trying to do is access, read and modify TB's properties from another class, such as App. That's all the code I have written,开发者_Python百科 still, no can do. I prefer not to assign the values from TB's properties to variables, but manage them directly.


The class in which you want to modify the TB, store the TB's reference in that class. Then using that reference you can modify the properties of TB.

do something like this..

 public class MyClass
    {
        Textbox m_TextBox;

        public MyClass(Textbox TB)
        {
        m_TextBox = TB;
        }

        ModifyTextbox()
        {
           m_TextBox.Text = "Hello World";
        }
    }

in the MainWindow.cs create a new instance of MyClass

   MyClass myClass = new MyClass(TB);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号