locked
UserControl and Class RRS feed

All replies

  • How to use a class variable in usercontrol in wpf?

    The UserControl is a class. All the variables that you declare inside are class variables. You just call them by using their name in your code.

    If you mean variables in another class, then there are two options:

    • If the variables are static, refer to them as NameOfTheClass.NameOfTheVariable. Of course, the variable needs to be sufficiently visible for this to work; typically you will make it public.
    • If the variables are instance variables, then you must have an instance reference in order to access the variables. If the instance is not available to your UserControl then it is impossible to access such variables. If you do have the instance, then refer to the variable as instanceReference.NameOfTheVariable.

    Of course, I am referring to accessing the variables from the C# code. Given that you mentioned WPF, I suspect that maybe what you want is to use the variable from XAML. In this case, it is better if you ask in the WPF forum instead of the C# forum.

    Sunday, May 10, 2020 1:41 PM
  • Please post questions related to WPF in the new Microsoft Q&A forums.

    Michael Taylor http://www.michaeltaylorp3.net

    Monday, May 11, 2020 1:19 PM