Datacontext conflicts

In a UserControl like this you should never exlicitly set the DataContext to this or anyting else, because the DataContext is usually set externally when you use the UserControl somewhere in your application. The externally applied DataContext is typically (part of) the application’s view model. You should instead change your internal bindings so that they … Read more

Simulating Cross Context Joins–LINQ/C#

Maybe something like this can get you started in the right direction. I made a mock database with similar columns based on your column names and got some results. class Program { static AccountContextDataContext aContext = new AccountContextDataContext(@”Data Source=;Initial Catalog=;Integrated Security=True”); static LoanContextDataContext lContext = new LoanContextDataContext(@”Data Source=;Initial Catalog=;Integrated Security=True”); static void Main() { var … Read more

How to make Entity Framework Data Context Readonly

In addition to connecting with a read-only user, there are a few other things you can do to your DbContext. public class MyReadOnlyContext : DbContext { // Use ReadOnlyConnectionString from App/Web.config public MyContext() : base(“Name=ReadOnlyConnectionString”) { } // Don’t expose Add(), Remove(), etc. public DbQuery<Customer> Customers { get { // Don’t track changes to query … Read more

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

Unlike most types which implement IDisposable, DataContext doesn’t really need disposing – at least not in most cases. I asked Matt Warren about this design decision, and here was his response: There are a few reasons we implemented IDisposable: If application logic needs to hold onto an entity beyond when the DataContext is expected to … Read more

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

The ContextMenu is outside of the visual tree. Below is the xaml that should get you the datacontext: <ItemsControl ItemsSource=”{Binding Markers}” Tag=”{Binding ElementName=outerControl, Path=DataContext}”> … <ContextMenu DataContext=”{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Self}}”> <MenuItem Header=”Edit” Command=”{Binding EditCommand}” /> </ContextMenu> … </ItemsControl> This post explains how this works.

page.DataContext not inherited from parent Frame?

You didn’t specifically ask how you could make this work, only why it doesn’t by default. However, if you do want your Pages to inherit the Frame’s DataContext, you can do this: In XAML: <Frame Name=”frame” LoadCompleted=”frame_LoadCompleted” DataContextChanged=”frame_DataContextChanged”/> In codebehind: private void frame_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e) { UpdateFrameDataContext(sender, e); } private void frame_LoadCompleted(object sender, NavigationEventArgs … Read more