tags:

views:

41

answers:

3

hello all

i have a problem, i have some data in Datacontext and i want to fetch this data how can i achive it

thanks in advance shashank

button1.DataContext = new DataView(tablename, "field1 in (" + stringbuilder.ToString() + "0)", "field1", DataViewRowState.CurrentRows);
A: 

hi, how you want to access it? from xaml or in code? or do you use an mvvm-like aproach?

ok from code, its as someone noted before:

if(button1.DataContext is DataView)  
{  
   DataView view = (DataView)button1.DataContext;  

   //do what you want with your DataView here  
}
SwissCoder
i want to fetch the data from code
Chunmun Tyagi
i think in ure case I would use that syntax..
SwissCoder
A: 

Who don't you fetch the data first before assigning it to the DataContext.

John
+1  A: 

The DataContext is a special field that works by setting the default binding target of an element and all of its sub-elements. So for example, you can bind to sub-properties of your DataContext by just specifying a path like so:

<StackPanel DataContext="{DynamicResource selectedBook}">
    <TextBlock Text="{Binding Path=Title}" />
    <TextBlock Text="{Binding Path=Author}" />
</StackPanel>

Of course, to get the DataContext from code, just access the DataContext property and cast it to whatever type you need:

MyClass context = (MyClass)this.DataContext;
....
chaiguy
Probably safer to use the `as` keyword and check for null, unless you're absolutely sure what it is.
chaiguy