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
29
votes
4 answers

How to instantiate DataContext object in XAML

I want to be able to create an instance of the DataContext object for my WPF StartupUri window in XAML, as opposed to creating it code and then setting the DataContext property programmaticly. The main reason is I don't need to access the object…
Gus Paul
  • 945
  • 2
  • 9
  • 17
28
votes
1 answer

C# Linq-to-Sql - Should DataContext be disposed using IDisposable

I have several methods that deal with DB and all of them start by calling FaierDbDataContext db = new FaierDbDataContext(); Since the Linq2Sql DataContext object implements IDisposable, should this be used with "using"? using (FaierDbDataContext db…
Kornelije Petak
  • 9,412
  • 15
  • 68
  • 96
26
votes
4 answers

UserControl as DataTemplate inside ListBox

I want to reuse my UserControls in other UserControls like page or window as DataTemplates, in this example inside a ListBox. Everything is MVVM. I've a UserControl called "CardControl" to display a simple object "Card". Card has two Properties,…
pDEV
  • 273
  • 1
  • 3
  • 5
26
votes
1 answer

Setting in XAML

I followed a very simple MVVM example as a basis for my program. The author had one code behind instruction he used in the main page to set the DataContext. I'm thinking I should be able to do this in the XAML instead. The MainWindowViewModel is in…
David Beck
  • 297
  • 1
  • 4
  • 6
25
votes
4 answers

Simulating Cross Context Joins--LINQ/C#

Here's the issue: I have 2 data contexts that I would like to do a join on. Now I know that LINQ doesn't allow joins from one context to another, and I know that 2 possible solutions would be to either create a single datacontext or to have 2…
David Yancey
  • 2,020
  • 1
  • 16
  • 21
24
votes
6 answers

How to hide a control if the underlying DataContext is null?

I have an object in my view model that has a bunch of properties, some of them will occasionally be null. I don't want to just show some controls if these particular controls are null. How would I go about hiding the control if the bind is null? I…
Harry
  • 3,076
  • 4
  • 28
  • 44
24
votes
2 answers

How to set d:DesignInstance to a generic type?

An easy one ;-) I declared: xmlns:om="clr-namespace:System.Collections.ObjectModel;assembly=System" I try to set a generic as the DataType:
Eric Ouellet
  • 10,996
  • 11
  • 84
  • 119
23
votes
5 answers

In LINQ-SQL, wrap the DataContext is an using statement - pros cons

Can someone pitch in their opinion about pros/cons between wrapping the DataContext in an using statement or not in LINQ-SQL in terms of factors as performance, memory usage, ease of coding, right thing to do etc. Update: In one particular…
hIpPy
  • 4,649
  • 6
  • 51
  • 65
22
votes
1 answer

Difference between ItemsSource and DataContext as pertains to ListBox

I am not quite grokking the difference between ItemsSource and DataContext. Can someone explain it and back it up with examples? When would I use one or the other. I am reading the docs and it says that I can bind using DataContext, but I throw an…
AngryHacker
  • 59,598
  • 102
  • 325
  • 594
22
votes
3 answers

Ninject and DataContext disposal

I'm using Ninject to retrieve my DataContext from the kernel and I was wondering if Ninject automatically disposes the DataContext, or how he handles the dispose() behaviour. From own experiences I know disposing the datacontext is pretty important…
Bas
  • 1,232
  • 1
  • 12
  • 26
22
votes
6 answers

Most efficient way to update with LINQ to SQL

Can I update my employee record as given in the function below or do I have to make a query of the employee collection first and then update the data? public int updateEmployee(App3_EMPLOYEE employee) { DBContextDataContext db = new…
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
21
votes
3 answers

DataContext and binding self as RelativeSource

Can someone explain me the following XAML line? DataContext="{Binding RelativeSource={RelativeSource Self}}" Here the simple example of use. How to replace that line with a C# code?
gliderkite
  • 8,828
  • 6
  • 44
  • 80
20
votes
3 answers

page.DataContext not inherited from parent Frame?

I have a Page page in a Frame frame, with frame.DataContext = "foo". (page.Parent as Frame).DataContext is "foo". ok BindingExpression for page.DataContext is null (also forced with ClearValue). ok page.DataContext is null. but I expected…
Marcel Jackwerth
  • 53,948
  • 9
  • 74
  • 88
18
votes
5 answers

How to use transactions with a datacontext

Can I use transactions with a datacontext, so that I can rollback the state of the context after an error? And if so, how does that work?
user107534
  • 201
  • 1
  • 2
  • 4
18
votes
7 answers

How to get the TSQL Query from LINQ DataContext.SubmitChanges()

I'm using Linq to SQL. I have a DataContext against which I am .SubmitChanges()'ing. There is an error inserting the identity field, and I'd like to see the query it's using to insert this identity field. I don't see the query itself within the…
tsilb
  • 7,977
  • 13
  • 71
  • 98
1
2
3
98 99