Questions tagged [datacontext]

A DataContext is used in WPF, Silverlight and various other .NET technologies. When used with WPF or Silverlight, a DataContext is an object holding the data that the form (or page in case of Silverlight) uses as a display source for its UI elements.

A DataContext is used in WPF, Silverlight and various other .NET technologies. When used with WPF or Silverlight, a DataContext is an object holding the data that the form (or page in case of Silverlight) uses as a display source for its UI elements. The DataContext in this case is the default object to bind against for any bindings that you have specified for other properties on the UI elements.

Ref: FrameworkElement.DataContext

1485 questions
236
votes
3 answers

WPF Databinding: How do I access the "parent" data context?

I have a list (see below) contained in a window. The window's DataContext has two properties, Items and AllowItemCommand. How do I get the binding for the Hyperlink's Command property needs to resolve against the window's DataContext?
Jordan
  • 2,811
  • 2
  • 20
  • 21
140
votes
7 answers

How to make Entity Framework Data Context Readonly

I need to expose an Entity Framework Data Context to 3rd party plugins. The purpose is to allow these plugins to fetch data only and not to let them issue inserts, updates or deletes or any other database modification commands. Hence how can I make…
Harindaka
  • 4,658
  • 8
  • 43
  • 62
53
votes
6 answers

UserControl's DataContext

I'm creating a UserControl I want to use something like this: So far, I've implemented similar controls like this:
svick
  • 236,525
  • 50
  • 385
  • 514
52
votes
6 answers

Enabling Service Broker in SQL Server 2008

I am integrating SqlCacheDependency to use in my LinqToSQL datacontext. I am using an extension class for Linq querys found here - http://code.msdn.microsoft.com/linqtosqlcache I have wired up the code and when I open the page I get this exception -…
JGilmartin
  • 8,683
  • 14
  • 66
  • 85
52
votes
6 answers

LINQPad, using multiple datacontexts

I am often comparing data in tables in different databases. These databases do not have the same schema. In TSQL, I can reference them with the DB>user>table structure (DB1.dbo.Stores, DB2.dbo.OtherPlaces) to pull the data for comparison. I like…
Scott
46
votes
3 answers

Entity Framework Thread Safety

The context objects generated by Entity Framework are not thread-safe. What if I use two separate entity contexts, one for each thread (and call SaveChanges() on each) - will this be thread-safe? // this method is called from several threads…
Harper
  • 1,285
  • 3
  • 15
  • 35
46
votes
10 answers

Setting a custom property within a WPF/Silverlight page

This sounds like it should be simple. I have a Page declared in XAML in the normal way (i.e. with "Add new item...") and it has a custom property. I'd like to set that property in the XAML associated with the page. Trying to do this the same way…
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
38
votes
4 answers

Should I return IEnumerable or IQueryable from my DAL?

I know this could be opinion, but I'm looking for best practices. As I understand, IQueryable implements IEnumerable, so in my DAL, I currently have method signatures like the following: IEnumerable GetProducts(); IEnumerable
Armstrongest
  • 15,181
  • 13
  • 67
  • 106
37
votes
2 answers

Why are DataContext and ItemsSource not redundant?

In WPF Databinding, I understand that you have DataContext which tells an element what data it is going to bind to and ItemsSource which "does the binding". But e.g. in this simple example it doesn't seem that ItemsSource is doing anything useful…
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
37
votes
4 answers

WPF ContextMenu woes: How do I set the DataContext of the ContextMenu?

I am having some trouble figuring out how to set the correct DataContext on a ContextMenu. I have a collection of view models who are the source of an ItemsControl. Each view model has a collection of items which are also the source of another…
Ashley Grenon
  • 9,305
  • 4
  • 41
  • 54
35
votes
2 answers

Use DataContext as CommandParameter in WPF

I want to pass the current DataContext (which is an instance of a ViewModel) as a CommandParameter on a WPF Button. What would be the syntax I should use?
32
votes
5 answers

Are Multiple DataContext classes ever appropriate?

In order to fully use LinqToSql in an ASP.net 3.5 application, it is necessary to create DataContext classes (which is usually done using the designer in VS 2008). From the UI perspective, the DataContext is a design of the sections of your database…
Yaakov Ellis
  • 40,752
  • 27
  • 129
  • 174
31
votes
3 answers

Can I stop the dbml designer from adding a connection string to the dbml file?

We have a custom function AppSettings.GetConnectionString() which is always called to determine the connection string that should be used. How this function works is unimportant to the discussion. It suffices to say that it returns a connection…
drs9222
  • 4,448
  • 3
  • 33
  • 41
30
votes
7 answers

Silverlight - Setting DataContext in XAML rather than in constructor?

How can I set the DataContext on my Grid in XAML, instead of in the constructor? Here is how I do it in the constructor (LayoutRoot is the XAML Grid defined in the XAML): this.LayoutRoot.DataContext = this.HPVM; I would prefer to do it right in the…
MattSlay
  • 9,115
  • 5
  • 43
  • 52
30
votes
18 answers

Unable to create an object of type 'MyContext'. For the different patterns supported at design time

I have ConsoleApplication on .NET Core and also I added my DbContext to dependencies, but howewer I have an error: Unable to create an object of type 'MyContext'. For the different patterns supported at design time, see…
Taifunov
  • 533
  • 1
  • 4
  • 9
1
2 3
98 99