datacontext

Bbinding combobox within dataform to view model property outside dataform's context

為{幸葍}努か 提交于 2019-11-28 13:05:49
I have two properties in my view model: //Relationship has property ReasonForEndingId private Relationship editRelationship; public Relationship EditRelationship { get { return editRelationship; } set { if (editRelationship != value) { editRelationship = value; RaisePropertyChanged(EditRelationshipChangedEventArgs); } } } //ReasonForLeaving has properties Reason & Id private IList<ReasonForLeaving> reasonsComboList { get; set; } public IList<ReasonForLeaving> ReasonsComboList { get { return reasonsComboList; } private set { if (reasonsComboList != value) { reasonsComboList = value;

How to access parent's DataContext from a UserControl

╄→гoц情女王★ 提交于 2019-11-28 12:03:51
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. 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 Parent property to get the parent, which you then can safe-cast to a FrameworkElement and if it is not null

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

好久不见. 提交于 2019-11-28 11:18:14
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 same DataContext. So the ContentControl should do it with its descendant elements right? I have a scenario where that is NOT the case and I would like to know WHAT is the cause of that misbehaviour ?! That you understand a bit more about it please read this thread ( dont NOT want to copy everything here) where the trouble starts...: WPF: Can not find

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

十年热恋 提交于 2019-11-28 09:54:05
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? 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 will prevent their binding from working, or your control won't work as expected because you rely on it not

Inconsistent accessibility problem [duplicate]

主宰稳场 提交于 2019-11-28 08:53:22
问题 This question already has answers here : Inconsistent accessibility error with the following c# code. Why? (3 answers) Closed 6 years ago . I am following Rob Conery MVC Storefront tutorial series and I get an Inconsistent accessibility error from the following constructor public SqlCatalogRepository(DB dataContext) : public class SqlCatalogRepository : ICatalogRepository { DB db; public SqlCatalogRepository() { db = new DB(); //turn off change tracking db.ObjectTrackingEnabled = false; }

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

醉酒当歌 提交于 2019-11-28 08:34:58
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 syntax will work in the <Window> tag to let me specify that view model. That is not possible unless the

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

怎甘沉沦 提交于 2019-11-28 08:28:16
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; mainView.Show(); } } or in XAML like this: Window1.xaml: <DockPanel> <StackPanel HorizontalAlignment=

How to use transactions with a datacontext

偶尔善良 提交于 2019-11-28 06:55:33
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? 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. A DataContext will pick up an ambient transaction by default, so it is just a matter of ensuring there is a Transaction in scope. The details become the main issue: What options do you need (e.g. isolation level)

How to configure mvc mini profiler with Linq to SQL?

醉酒当歌 提交于 2019-11-28 04:47:47
问题 I have configured mini profiler with asp.net mvc application. I also want to profile my db so I hooked it with L2S datacontext as in this example. It is working fine for some queries but on other queries I find null reference exception. When I attached the source code to debug I found out that internal void AddSqlTiming(SqlTiming stats) { Head.AddSqlTiming(stats); } Head Property in above method is null in MiniProfiler.cs at line 198. Any idea why? EDIT: Following method returns me the

WPF Binding Syntax Question

我是研究僧i 提交于 2019-11-28 04:39:18
问题 I've seen this syntax show up, and have tried to google for it's definition to no avail; what does it mean when a dp is bound this way? <Grid> <ContentControl Content="{Binding}"/> </Grid> I was under the assumption that you have to bind to some property on the DataContext, or another element, but this appears to bind to nothing. 回答1: I believe it means you are binding to the root of whatever the binding context is. So if you use this syntax in a datatemplate that is part of some sort of list