datacontext

How to access parent's DataContext from a UserControl

为君一笑 提交于 2019-11-27 06:49:59
问题 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 parameter to user control but think there is a cleaner way to do it. 回答1: Normally the DataContext will be inherited, just do not explicitly set it on the UserControl and it will get it from its parent. If you have to set it you could still use the

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

六眼飞鱼酱① 提交于 2019-11-27 06:40:10
问题 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 Server Consider this LINQ To SQL query whose intention is to take a Customer entity. Cust custOrig = db.Custs.SingleOrDefault(o => o.ID == c.ID); //get the original db.Custs.Attach(c, custOrig); //we don't have a TimeStamp=True property db.SubmitChanges();

How to make Entity Framework Data Context Readonly

六眼飞鱼酱① 提交于 2019-11-27 06:04:24
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 a data context or entity 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

Difference between ItemsSource and DataContext as pertains to ListBox

狂风中的少年 提交于 2019-11-27 05:25:26
问题 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 ObservableCollection at it and nothing shows up in the list. If I throw the same collection at the ItemsSource, it works fine. 回答1: Controls (including the ListBox) don't do anything with the value of DataContext at all. Its purpose is to provide a

LINQPad, using multiple datacontexts

偶尔善良 提交于 2019-11-27 05:16:31
问题 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 the idea of LINQPad quite a bit, but I just can't seem to easily pull data from two different data contexts within the same set of statements. I've seen people suggest simply changing the connection string to pull the data from the other source

UserControl's DataContext

狂风中的少年 提交于 2019-11-27 03:44:00
I'm creating a UserControl I want to use something like this: <controls:ColorWithText Color="Red" Text="Red color" /> So far, I've implemented similar controls like this: <UserControl x:Class="Namespace.ColorWithText" Name="ThisControl"> <StackPanel Orientation="Horizontal" > <Border Width="15" Height="15" Background="{Binding Color, ElementName=ThisControl}" /> <TextBlock Text="{Binding Text, ElementName=ThisControl}" /> </StackPanel> </UserControl> where Color and Text are dependency properties of the control defined in code. This works, but specifying ElementName every time seems

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

家住魔仙堡 提交于 2019-11-27 03:23:11
问题 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? 回答1: In general, when someone uses your control they are going to want to set it's data context to their own view model class and bind the properties on your control to their view model. If you start messing around with the data context internally within the control, and rely on it being set to 'this', either you

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

偶尔善良 提交于 2019-11-27 02:19:56
问题 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) . <Window ... xmlns:d="http://schemas.microsoft.com/expression/blend/2008" d:DataContext="{d:DesignInstance Dialogs:CustomerSearchDlogViewModel}" ...> Now instead of CustomerSearchDlogViewModel I have a generic SearchDialogViewModel but I can't figure out what

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

人走茶凉 提交于 2019-11-27 02:15:45
问题 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) { // Create the ViewModel and expose it using the View's DataContext MainView mainView = new MainView(); MainViewModel mainViewModel = new MainViewModel(); mainViewModel.LoadCustomers("c:\\testdata2\\Customers.xml"); mainView.DataContext = mainViewModel;

How to use transactions with a datacontext

二次信任 提交于 2019-11-27 01:42:21
问题 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? 回答1: I use them in testing all the time :) try { dc.Connection.Open(); dc.Transaction = dc.Connection.BeginTransaction(); dc.SubmitChanges(); } finally { dc.Transaction.Rollback(); } UPDATE This will ALWAYS rollback after the fact. I use this in testing. 回答2: A DataContext will pick up an ambient transaction by default, so it is just a matter of ensuring