none
Access XAML object programmatically

    Question

  • Okay, I declared an instance of a custom class, DataHolderClass, as a local resource under as;
    <Window.Resources>
            <local:DataHolderClass x:Key="DHC"/>    </Window.Resources>
    However, I can't access it programmatically. I believe I have been able to bind to this resources in another line of code but I don't know, though there are no compile errors. Hence, I'm trying to call a function in the class inside of a button click method for a button on the GUI. However, intellisense does not recognize the key name though it does list the class type. Even more strange is that other classes that are declared only in the XAML, such as textboxes, do some up from intellitype. So, my question is how to access a local instance of a class that was declared in XAML.
    Wednesday, August 26, 2009 3:18 AM

Answers

  • Hi,
    -->However, I can't access it programmatically
    If you have already defined a class instance in XAML markup, you can programmatically get the instance by the resource key, something like this:
    ClassType item = this.Resources["classKey"] as ClassType;

    -->I'm trying to call a function in the class inside of a button click method for a button on the GUI. 
    In the Click event handler, you can instantiate an instance of the class, right? then you just call the method defined within the class.

     -->intellisense does not recognize the key name though it does list the class type
    yes, as far as I know, in the current version of WPF, XAML markup does not support key intellisense. You need to type the key manually.

    -->my question is how to access a local instance of a class that was declared in XAML.
    The following is a small sample for your reference.
    XAML markup:

    <Window.Resources>

            <local:ClassType  x:Key="classKey" Name="default value" />

        </Window.Resources>

    <TextBlock Text="{Binding Path=Name, Source={StaticResource classKey}}"  />

    Class definition:
    public
    class ClassType

        {

            public string  Name { get; set; }

    }

    If you are still having additional issues with this, please feel free to let me know.
    Thanks.


    Jim Zhou -MSFT
    Monday, August 31, 2009 10:16 AM

All replies

  • Hi,
    -->However, I can't access it programmatically
    If you have already defined a class instance in XAML markup, you can programmatically get the instance by the resource key, something like this:
    ClassType item = this.Resources["classKey"] as ClassType;

    -->I'm trying to call a function in the class inside of a button click method for a button on the GUI. 
    In the Click event handler, you can instantiate an instance of the class, right? then you just call the method defined within the class.

     -->intellisense does not recognize the key name though it does list the class type
    yes, as far as I know, in the current version of WPF, XAML markup does not support key intellisense. You need to type the key manually.

    -->my question is how to access a local instance of a class that was declared in XAML.
    The following is a small sample for your reference.
    XAML markup:

    <Window.Resources>

            <local:ClassType  x:Key="classKey" Name="default value" />

        </Window.Resources>

    <TextBlock Text="{Binding Path=Name, Source={StaticResource classKey}}"  />

    Class definition:
    public
    class ClassType

        {

            public string  Name { get; set; }

    }

    If you are still having additional issues with this, please feel free to let me know.
    Thanks.


    Jim Zhou -MSFT
    Monday, August 31, 2009 10:16 AM
  • Thank you. I got it to work so I can access it now. However I have a new problem. I'm trying to use bindings to update a custom class that I made. I'm just want to update a String object in the class. I'm using the following code to bind to the custom object, from a textbox:

    Text="{Binding ElementName=DHC, Mode=OneWayToSource, Path=Parish, UpdateSourceTrigger=PropertyChanged}"

    However, when I print the contents of the String it is blank, so for some reason it's not being updated. I think that the problem is I need to create a ValueConverter to take do the binding. I'm not sure.
    Tuesday, September 01, 2009 3:39 PM
  • Hi MasterCole,

    I am glad that your original issue was resolved yet. For your new question, I suggest you create a new thread and post the ready-to-run code sample, so that we can quickly repro your scenario and you can get the desired explanation or solution more quickly.

    Sincerely.
    Thanks.


    Jim Zhou -MSFT
    Wednesday, September 02, 2009 8:08 AM