0

This should be a beginner's question to WPF but I can't find a simple answer. How do you get a handle on an object declared in WPF?

<Window x:Class="Testprj" xmlns:local="clr-namespace:Testprj">
    <Window.Resources>
        <local:CustomValidation x:Key="validationObj"/>
    </Window.Resources>
</Window>

If I'm not mistaken, the above code is creating an instance of a "CustomValidation" object with the key "validationObj". Now, say each "CustomValidation" object has an instance variable called "myInstanceVar", how can I get the value of myInstanceVar from the code behind?

What I'm trying to do is something like (pseudocode):

validationObj.myInstanceVar

But it's unable to find even "validationObj". Any ideas?

David
  • 15,652
  • 26
  • 115
  • 156

2 Answers2

2

I believe you're asking how you can use that object from the code behind. I believe this.FindResource("validationObj") would do the trick.

var valObj = (CustomValidation)this.FindResource("validationObj");
var whatever = valObj.myInstanceVar;
Steve
  • 6,334
  • 4
  • 39
  • 67
0

you ask how to access control declared in XAML in codebehind

use x:name

than use poo in your codebehind

http://msdn.microsoft.com/en-us/library/ms752290.aspx

generally you should not have a code behind and use the MVVM design pattern to avoid it.

this is a good place to start: MVVM: Tutorial from start to finish?

Community
  • 1
  • 1
Nahum
  • 6,959
  • 12
  • 48
  • 69