datacontext

WPF Update Binding when Bound directly to DataContext w/ Converter

只愿长相守 提交于 2019-11-30 09:24:49
Normally when you want a databound control to 'update,' you use the "PropertyChanged" event to signal to the interface that the data has changed behind the scenes. For instance, you could have a textblock that is bound to the datacontext with a property "DisplayText" <TextBlock Text="{Binding Path=DisplayText}"/> From here, if the DataContext raises the PropertyChanged event with PropertyName "DisplayText," then this textblock's text should update (assuming you didn't change the Mode of the binding). However, I have a more complicated binding that uses many properties off of the datacontext to

Access DataContext behind IQueryable

一世执手 提交于 2019-11-30 08:45:00
Is it possible to access the DataContext object behind an IQueryable? If so, how? DataContext is specific to LINQ to SQL, so presumably you're talking about LINQ to SQL queries? If so, there's no safe way to do this - you have to resort to a hack such as using reflection to retrieve the private "context" field of the underlying DataQuery object: static DataContext GetContext (IQueryable q) { if (!q.GetType().FullName.StartsWith ("System.Data.Linq.DataQuery`1")) return null; var field = q.GetType().GetField ("context", BindingFlags.NonPublic | BindingFlags.Instance); if (field == null) return

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

回眸只為那壹抹淺笑 提交于 2019-11-30 08:05:01
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 was thinking of some sort of converter, but don't know how I'd go about doing it exactly. Any ideas? edit: sorry, I should mention that this will also be in Silverlight, so I'm not sure if Style triggers would work...? Prince Ashitaka Have a converter like follows, public sealed class NullToVisibilityConverter : IValueConverter { public object

How to bind column header to property in ViewModel? (WPF MVVM)

送分小仙女□ 提交于 2019-11-30 07:33:07
问题 I Have window that have DataContext that is bind to ViewModel object (VM1 for the example). VM1 have a lot of properties and one of them is a string that called "MyTitle". I have a DataGridTextColumn in 'Window\Grid\DataGrid'. How can I bind the property "Header" in DataGridTextColumn to the Property "MyTitle" in my VM1 ViewModel? Thanks! 回答1: Unfortunately, the column definitions of the DataGrid don't inherit the DataContext , because they're not part of the visual tree, so you can't bind

Linq: Get a list of all tables within DataContext

六月ゝ 毕业季﹏ 提交于 2019-11-30 07:26:30
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. It's much easier than above and no reflection required. Linq to SQL has a Mapping property that you can use to get an enumeration of all the tables. context.Mapping.GetTables(); You can do this via reflection. Essentially, you iterate over the properties in your DataContext class. For each property, check to see if that property's generic parameter type has the TableAttribute attribute. If so, that property

Exceptions by DataContext

蹲街弑〆低调 提交于 2019-11-30 06:54:12
I've been doing some searching on the internet, but I can't seem to find the awnser. What exceptions can a DataContext throw? Or to be more specific, what exceptions does the DataContext.SubmitChanges() method throw? EDIT For reference, here a List of possible known exceptions that could be thrown by the L2S DataContext: SqlException ChangeConflictException DuplicateKeyException ForeignKeyReferenceAlreadyHasValueException OutOfMemoryException (when not correctly disposing the DataContext) You're right, MSDN is not a great help here. This is what I can remember from the top of my head:

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

旧街凉风 提交于 2019-11-30 06:01:18
问题 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

How to instantiate DataContext object in XAML

谁都会走 提交于 2019-11-30 04:27:25
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 created externally and I don't want to have to write code behind just for setting the DataContext . I'm sure I've read somewhere how to instantiate the DataContext object in XAML but I can't find it in any of the usual places... You add an XML namespace for whatever namespace your DataContext lives in, create an instance of it in the Window Resources

Most efficient way to update with LINQ to SQL

爷,独闯天下 提交于 2019-11-30 03:06:05
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 DBContextDataContext(); db.App3_EMPLOYEEs.Attach(employee); db.SubmitChanges(); return employee.PKEY; } Or do I have to do the following? public int updateEmployee(App3_EMPLOYEE employee) { DBContextDataContext db = new DBContextDataContext(); App3_EMPLOYEE emp = db.App3_EMPLOYEEs.Single(e => e.PKEY == employee.PKEY); db.App3_EMPLOYEEs.Attach(employee,emp); db

Inject same DataContext instance across several types with Unity

自闭症网瘾萝莉.ら 提交于 2019-11-30 02:29:00
Suppose I have IRepository interface and its implementation SqlRepository that takes as an argument LINQ to SQL DataContext. Suppose as well that I have IService interface and its implementation Services that takes three IRepository, IRepository and IRepository. Demo code is below: public interface IRepository<T> { } public class SqlRepository<T> : IRepository<T> { public SqlRepository(DataContext dc) { ... } } public interface IService<T> { } public class Service<T,T1,T2,T3> : IService<T> { public Service(IRepository<T1> r1, IRepository<T2>, IRepository<T3>) { ... } } Is it any way while