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
17
votes
2 answers

What does LINQ-to-SQL Table.Attach do?

What exactly does the LINQ-to-SQL method Table.Attach() and Table.AttachAll() and what is an example/situation for their proper usage? Also, please check out this related question: How to detach a LINQ-to-SQL data object from the DataContext's…
smartcaveman
  • 41,281
  • 29
  • 127
  • 212
17
votes
3 answers

DataContext Accessed After Dispose

I'm using ASP.NET 4.0. I've got the following code that returns with an error of "Cannot access a disposed object. Object name: 'DataContext accessed after Dispose.'." public IEnumerable GetHeaders() { …
Aaron
  • 7,431
  • 12
  • 35
  • 37
16
votes
2 answers

How do I set WPF xaml form's Design DataContext to class that uses generic type parameters

Originally my .xaml form used the following line to set the Designer's DataContext where the view model was a non-generic type (note I'm talking about the Design time DataContext not the actual DataContext that will be used at runtime).
Tod
  • 8,192
  • 5
  • 52
  • 93
16
votes
2 answers

Setting DataContext within UserControl is affecting bindings in parent

I have a basic UserControl that sets its DataContext to itself for ease of binding:
GazTheDestroyer
  • 20,722
  • 9
  • 70
  • 103
16
votes
3 answers

Why Is React Context.Provider Necessary (Or useful)?

The reason React has contexts is to allow for multiple sibling components to share a piece of state-data. It is the go-to method for allowing two unrelated components to read/write in shared variables. The reason it is necessary is that React has no…
ICW
  • 4,875
  • 5
  • 27
  • 33
16
votes
2 answers

Exceptions by DataContext

I've been doing some searching on the internet, but I can't seem to find the awnser. What exceptions can a DataContext throw? Or to be more specific, what exceptions does the DataContext.SubmitChanges() method throw? EDIT For reference, here a List…
Bas
  • 1,232
  • 1
  • 12
  • 26
15
votes
5 answers

LinqToSql declare and instantiate DataContext best practice?

What's the best practice in terms of setting up my DataContext for easy access in my extended LinqToSql classes? For example, I have a "User" entity in my dbml and I want to add methods to that class like so: Partial Public Class User Public…
RichC
  • 7,829
  • 21
  • 85
  • 149
15
votes
4 answers

Why would reusing a DataContext have a negative performance impact?

After a fair amount of research and some errors, I modified my code so that it creates a new DataContext each time the database is queried or data is inserted. And the database is queried frequently - for each of 250k transactions that are…
MCS
  • 22,113
  • 20
  • 62
  • 76
15
votes
2 answers

LINQ To SQL exception with Attach(): Cannot add an entity with a key that is already in use

Consider this typical disconnected scenario: load a Customer object from SQL Server using LINQ To SQL user edits the entity, and the presentation tier sends back the entity modified. the data layer, using L2S, must send the changes to SQL…
p.campbell
  • 98,673
  • 67
  • 256
  • 322
14
votes
3 answers

set xaml code ItemsSource="{Binding}" with code behind

I have the following property Temp2: (my UserControl implements INotifyPropertyChanged) ObservableCollection _Temp2; public ObservableCollection Temp2 { get { return _Temp2; } …
Tono Nam
  • 34,064
  • 78
  • 298
  • 470
14
votes
7 answers

What is the advantage of setting DataContext in code instead of XAML?

There seem to be two main ways to define DataContext in WPF: either in code like this: App.xaml.cs (taken from the WPF MVVM Toolkit template): public partial class App : Application { private void OnStartup(object sender, StartupEventArgs e) …
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
13
votes
1 answer

ItemSource vs Datacontext in wpf

Can any one give me the difference between Itemsource and DataContext of Listview in WPF? With example
Sauron
  • 16,668
  • 41
  • 122
  • 174
13
votes
1 answer

What's wrong with "DataContext = this" in WPF user controls?

I read somewhere that setting DataContext = this in the constructor of a user control is bad practice (can't find where though). Why is this bad practice? What is the alternative?
Deniz Dogan
  • 25,711
  • 35
  • 110
  • 162
12
votes
3 answers

WPF binding with PlacementTarget and RelativeSource

Can you explain the following WPF code: DataContext="{Binding Path=PlacementTarget,RelativeSource={x:Static RelativeSource.Self}}"> I find it extremely confusing. What is placement target and what is relative source?
MadSeb
  • 7,958
  • 21
  • 80
  • 121
12
votes
1 answer

Access DataContext behind IQueryable

Is it possible to access the DataContext object behind an IQueryable? If so, how?
Alex
  • 75,813
  • 86
  • 255
  • 348
1 2
3
98 99