NOCARRIER
NOCARRIER

Reputation: 2634

How to retrieve the data out of a Silverlight DataContext Object

I have a Silverlight application which is a collection of form fields and buttons.

I've created a method stub that handles a click event in the xaml.cs. When I inspect sender during debug, I can see the base type is a TextBlock, and in the DataContext object within that textblock I see my custom type's properties. One of them is GUID - this is the Sender's type, so I cast to a TextBlock and I can see the DataContext, but I am not sure how to get my type's field value out of this object:

private void someTextField_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {

        var dataContext = (TextBlock) sender;
        var assetGUID = dataContext.DataContext.  /
        // intellsense does not show any fields, indexers, or getters - Just says "Get or Set datacontext fields in a datacontext".

    }

As stated, if I debug and place a watch on Sender, go two levels deep I can see my objects fields.

Thanks.

Upvotes: 1

Views: 793

Answers (1)

TerenceJackson
TerenceJackson

Reputation: 1786

if you can see in Debug mode, that the DataContext of the TextBlock is your needed Object, then you just have to cast it to your object.

private void someTextField_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {

        var dataContext = (TextBlock) sender;
        var assetGUID = ((YourObject)dataContext.DataContext).getGuid()  /
        // intellsense does not show any fields, indexers, or getters - Just says "Get or Set datacontext fields in a datacontext".

    }

You need to do this, because DataContext is defined with the return value Object (DataContext)

Is this what you need?

BR,

TJ

Upvotes: 4

Related Questions