datacontext

Context Menu Binding to Parent Window's Datacontext

落花浮王杯 提交于 2019-11-29 02:43:34
I have a TreeListControl that binds to a collection in my VM. I also want to define the context menu inside the treelistcontrol having its header text bind to another string in my VM. how can I set the data context in this case? I tried to <Window.DataContext> <model:ViewModel></model:ViewModel> </Window.DataContext> <Grid> <Button Grid.Row="1" Command="{Binding CellCheckedCommand}"></Button> <TextBlock Text="{Binding HeaderText}" Grid.Row="2"> <TextBlock.ContextMenu> <ContextMenu> <MenuItem DataContext="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=DataContext}"

How to instantiate DataContext object in XAML

戏子无情 提交于 2019-11-29 01:44:05
问题 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... 回答1: You add an XML

UserControl as DataTemplate inside ListBox

你说的曾经没有我的故事 提交于 2019-11-29 00:21:23
问题 I want to reuse my UserControls in other UserControls like page or window as DataTemplates, in this example inside a ListBox. Everything is MVVM. I've a UserControl called "CardControl" to display a simple object "Card". Card has two Properties, "ID" and "CardImage". The controls DataContext is set via XAML. If I open this UserControl in VS or Blend it shows me the dummy Card that I have defined in the corresponding ViewModel. Now I have another UserControl called "CardSetControl", which

Most efficient way to update with LINQ to SQL

十年热恋 提交于 2019-11-29 00:10:19
问题 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 =

Inject same DataContext instance across several types with Unity

无人久伴 提交于 2019-11-28 23:26:05
问题 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> {

Ninject caching an injected DataContext? Lifecycle Management?

China☆狼群 提交于 2019-11-28 19:56:46
I had a series of very bizarre errors being thrown in my repositories. Row not found or changed, 1 of 2 updates failed... Nothing made sense. It was as if my DataContext instance was being cached... Nothing made sense and I was considering a career move. I then noticed that the DataContext instance was passed in using dependency injection, using Ninject (this is the first time I have used DI...). I ripped out the Dependency Injection, and all went back to normal. Instantly. So dependency injection was the issue, but I still don't know why. I am speculating that Ninject was caching the injected

Why would reusing a DataContext have a negative performance impact?

丶灬走出姿态 提交于 2019-11-28 19:38:31
After a fair amount of research and some errors , I modified my code so that it creates a new DataContext each time the database is queried or data is inserted. And the database is queried frequently - for each of 250k transactions that are processed, the database is queried to obtain a customer id, department id, and category before the transaction is inserted. So now I'm trying to optimize the code as it was only processing around 15 transactions a second. I removed some extraneous queries and added some indexes and got it up to 30/sec. I then figured that even though everyone says a

Why are DataContext and ItemsSource not redundant?

烂漫一生 提交于 2019-11-28 17:13:29
In WPF Databinding, I understand that you have DataContext which tells an element what data it is going to bind to and ItemsSource which "does the binding". But e.g. in this simple example it doesn't seem that ItemsSource is doing anything useful since, what else would you want the Element to do to the DataContext except bind to it ? <ListBox DataContext="{StaticResource customers}" ItemsSource="{Binding}"> And in more complex examples of ItemsSource , you have Path and Source which seems to be encroaching on the territory of DataContext . ItemsSource="{Binding Path=TheImages, Source=

Should I return IEnumerable<T> or IQueryable<T> from my DAL?

橙三吉。 提交于 2019-11-28 15:06:35
问题 I know this could be opinion, but I'm looking for best practices. As I understand, IQueryable<T> implements IEnumerable<T> , so in my DAL, I currently have method signatures like the following: IEnumerable<Product> GetProducts(); IEnumerable<Product> GetProductsByCategory(int cateogoryId); Product GetProduct(int productId); Should I be using IQueryable<T> here? What are the pros and cons of either approach? Note that I am planning on using the Repository pattern so I will have a class like so

Entity Framework 4.1: how to work with per call life time data context?

混江龙づ霸主 提交于 2019-11-28 14:21:23
According to this post , I am using a data context per call, so in each method of my WCF service, I use a using block to create a new data context. But I have some doubts in the form to work in this way. For example, I use a method getAllCLients() from my repository to get all the clients of the data base, then the service send to the client that call the method a list with all the clients. Then the user modify the information of some of them, three for example. The modify client perhaps I can add to a list that have the modified clients. When I want to update this three clients, I can call a