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
12
votes
1 answer

Setting datacontext of UserControl to ViewModel defined in parent viewmodel

I'm trying to create an application using the MVVM pattern with nested viewmodels. The master viewmodel is ShellView which contains three UserControls, each with their own viewmodel. The ShellView ViewModel is created in code-behind like so: public…
ZymLink
  • 157
  • 1
  • 1
  • 8
11
votes
2 answers

Determine if a WPF DependencyProperty value is inherited

Does anyone know how to determine if the value of a WPF property is inherited? In particular, I'm trying to determine if the DataContext of a FrameworkElement was inherited from the parent or set directly on the element itself.
Michael Hewitt
  • 275
  • 1
  • 2
  • 8
11
votes
4 answers

Linq: Get a list of all tables within DataContext

I have a DataContext (Linq to Sql) with over 100 tables, is it possible to get a list of all those tables and lets say print them to the console? This might be a silly question. Thanks.
Sergey
  • 3,214
  • 5
  • 34
  • 47
11
votes
5 answers

How to access parent's DataContext from a UserControl

I need to access the container's DataContext from a UserControl (a grid containing textboxes and a listbox: I need to insert items in this list box) that I created in WPF: which is the best way to do it? I was thinking to pass the DataContext as…
Cris
  • 12,124
  • 27
  • 92
  • 159
11
votes
4 answers

Where should I set the DataContext - code behind or xaml?

(honestly I searched and read all the 'related questions' that seemed relevant - i do hope i didn't "miss" this question from elsewhere but here goes...) There are two different ways (at least) to set the DataContext. One can use XAML or one can…
dovholuk
  • 969
  • 1
  • 11
  • 23
11
votes
4 answers

LINQ to SQL multiple DataContext-s

I'm trying to figure the best strategy about how to organize DataContexts. The typical DB we work has between 50 and 100 tables usually in third-normal form and with many relations between them. I think we have two options: Put all tables in a…
Albert
  • 1,015
  • 2
  • 10
  • 28
11
votes
3 answers

Multiple dataContext for one control - MVVM

I am not sure if my question header represent exactly my problem, I will do the best to explain: I have a grid cell DataTemplate: (the grid belong to third party company but it`s not important for my question)
Ofir
  • 5,049
  • 5
  • 36
  • 61
10
votes
2 answers

Event for "DataContextChanging"?

I am very new to WPF so forgive me if the question doesn't make sense. Is there an event that is fired before data context change? I want to commit the pending data changes before the data context is switched away.
NS.X.
  • 2,072
  • 5
  • 28
  • 55
10
votes
2 answers

Differences between .CreateObjectSet, .Set, and .CreateQuery?

I am writing a generic repository for entity framework and am confused as to what the difference between these calls are: ObjectContext.CreateObjectSet ObjectContext.CreateQuery DbContext.Set I want a generic repository that both supports…
SventoryMang
  • 10,275
  • 15
  • 70
  • 113
10
votes
2 answers

wpf how to bind to DataContext existence?

I set the datacontext dynamically in code. I would like a button on screen to be enabled/disabled depending if DataContext == null or not. I can do it in code when I assign the DataContext but it would be better if I can bind like that :)
katit
  • 17,375
  • 35
  • 128
  • 256
10
votes
4 answers

Working with Cross Context Joins in LINQ-to-SQL

Initially I had written this query using LINQ-to-SQL var result = from w in PatternDataContext.Windows join cf in PatternDataContext.ControlFocus on w.WindowId equals cf.WindowId join p in PatternDataContext.Patterns on cf.CFId equals…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
10
votes
4 answers

FrameworkElement`s DataContext Property does NOT inherit down the element tree

Hello WPF Pros at least I hope some of you read this! DataContext is a property on FrameworkElement (base class for all WPF Controls) and is implemented as a DependencyProperty. That means all the descendant elements in the logical tree share the…
Elisabeth
  • 20,496
  • 52
  • 200
  • 321
10
votes
3 answers

Wpf datacontext binding using MVVM between viewmodel and view

I just started learning MVVM and here is what seems to be basic question but I spent whole day trying to figure it out. I have a solution that contains 3 projects one for Model, one for ViewModel and one for View. The Model contains a class that has…
user7900863
10
votes
2 answers

Binding works fine, but intellisense says: Cannot resolve property XXX in data context of type 'object'

I have a binding to a parent-element. How can I provide the data type for the DataContext in the binding, so intellisense can resolve the bound Properties? The binding works fine at runtime. So, I have the following XAML structure:
ptair
  • 177
  • 1
  • 11
9
votes
2 answers

Ignore read-only class properties when using DataContext.ExecuteQuery

How do I tell a LINQ data context to ignore either specific properties, or all readonly properties, when binding a result set to an object? I am working with some T-SQL statements that are difficult to express using LINQ, so I'm using the…
Seth Petry-Johnson
  • 11,845
  • 7
  • 49
  • 69