.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Binding WPF grid to ViewModel : Suggest the design

Posted By:      Posted Date: September 02, 2010    Points: 0   Category :WPF
I have structure like this I have following structure BaseViewModel ViewModel1: BaseviewModel ViewModel2: BaseViewModel In grid i will be showing the data from both the ViewModel1 and ViewModel2 (some common data is there ) How should i go about this Expected functionality : IF i select any row in the grid i should get respective ViewModel    

View Complete Post

More Related Resource Links

Design Patterns: Problems and Solutions with Model-View-ViewModel


The Model-View-ViewModel (MVVM) design pattern describes a popular approach for building Windows Presentation Foundation or Microsoft Silverlight applications. Robert McCarter shows you how the ViewModel works, and discuss some benefits and issues involved in implementing a ViewModel in your code.

Robert McCarter

MSDN Magazine July 2010

Patterns: WPF Apps With The Model-View-ViewModel Design Pattern


In this article we explain just how simple it can be to build a WPF application the right way using the MVVM Pattern.

Josh Smith

MSDN Magazine February 2009

Data Binding: Give Your Everyday Custom Collections a Design-Time Makeover


In this article, Paul Ballard shows how you can provide data binding support for your custom collections to enable sorting, searching, and editing in as simple a manner as possible. In addition, he shows how to make all of these features available in the Windows and Web Forms Designers, just like an ADO.NET DataSet.

Paul Ballard

MSDN Magazine August 2005

Design binding help with MVVM

Dear all, I have the follwoing View model which collect a list of Winners from a database define as follow :  public class WinnersViewModels : DependencyObject { public WinnerSet WinnerSet{get;set;} public CollectionView WinnersCollection { get; set; } public WinnersViewModels() { WinnerSet = new WinnerSet(); WinnerSet.CallRandomWinners(); WinnersCollection = (CollectionView)CollectionViewSource.GetDefaultView(WinnerSet.Winners); } }  WinnerSet is a simple class which collect information from a Winner and Winners a collection of WinnerSet as follow : public class WinnerSet { private string connectionString = string.Empty; public string FirstName {get;set;} public string LastName { get; set; } public string Company { get; set; } public Winners Winners { get; set; } ....... } public class Winners : ObservableCollection<WinnerSet> { } What I need to do out of this is to be able to add in  a list box the list of Winners return by the WinnersViewModels. For that I have define a user control which define my different fields like firstName, LastName, Company and set dynamically the content of the listbox Item to be my user control and the datacontext to be the current item. Doing so my user control appears in the list box but the binding do not occu

Binding ActualHeight and ActualWidth to ViewModel

Hi, Currently we are doing an application where we need to resize the content based on current height and width using MVVM. But the problem is since ActualHeight and ActualWidth is not a dependency property we were not able to bind it to viewmodel as we need the height and width for replotting a map. Could you please help me on this issue Regards, Raaj

Binding Grid to Excel Doc

I used the below code, and all i get is empty rows of data for each of the represented rows in my Excel doc. Am I doing something wrong?: private void btnUploadExcelToGrid_Click(object sender, RoutedEventArgs e) { string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileSouce + ";Extended Properties=Excel 8.0;"; OleDbConnection conn = new OleDbConnection(strConn); OleDbDataAdapter da = new OleDbDataAdapter(); da.SelectCommand = new OleDbCommand("SELECT * FROM [Sheet1$]", conn); DataTable dt = new DataTable(); da.Fill(dt); dgExcel.DataContext = dt; } I do not have ACE installed on my dev box, and I am only using office 2k3, so Excel 8.0 made sense to me.  Here is a pic of what I get back: http://www.facebook.com/photo.php?pid=547091&l=32e9fb3745&id=1811331443  Charles Charles

Binding to a grid row height, with splitter works until splitter is used.


I hava a grid with two rows and a splitter, and it works fine.

I want the ability to show only one row(make the other one collapse), so I created binding between the second row height to a member in my view model.

When something happen(button click) I change the member value in my view model and the row disappear. on next click the row comes back and everything works fine.

The problem is that if I press button and use splitter, the binding stops working and on next clicks nothing happens.


I know I can write a specific ui side code that work without the binding, I prefer not to.

Help please.

Binding a Grid via WCF Data Services


Hello everybody,

i am trying to bind a grid to a database via the internet. I did this for years and every time i do it, i am looking for new ways to do it. So this time i will give WCF Data Services a chance. But wait... after the first "solutions" i tried i figured out that nothing changed the years. I seems to be not easy at all....

The Idea of binding a grid to a database is, that the user see's data, can filter, search and sort on each column. And because we don't know the amount of rows we need a way to only load "some" of the rows and refetch at a time. This is a minimum requirement for each grid. So this is the goal. This means in case of the internet, that the query must be transferred to the server and the server should only return the data that was requested. It also means, that the grid must fetch only "some" rows and refetch the rest of the rows when the user is scrolling down.

Can WCF Data Services do that? Can the grid of .NET do that? Can the Telerik Grid do that or any other grid? 

I tried some of the examples called "binding grid to wcf data services", but none of them could do it. Maybe someone has a simple solution for it? Or do i have to write that my own?

Thanks for your help ... regards




EF Binding to DataGrid at design time in VS2010


I'm playing arround with the DataGrid for wpf and EF in Visual studio 2010.
The easiest way to bind is to set the ItemsSource="{Binding} and in the Window_Loaded event MyDataGrid.DataContext = efData.SomeData
It is working fine, but I don't have data at design time.
Is there an easy way todo?


Getting multiple rows from oracle stored procedure and binding it to grid view



To  get this stuff you need to create a Ref Cursor to return recordset.

             create or replace PACKAGE Types AS

            TYPE cursor_type IS REF CURSOR;

            END Types;

Now create a stored procedure to return multiple rows .

           create or replace

           PROCEDURE getAllCity(p_recordset OUT types.cursor_type) AS


           OPEN p_recordset FOR


Problem with resizing images and padding them to squared images before binding them to grid



I am developing a web application in ASP.NET and C# using Visual Studio 2008. The application will among other thing store images into a database in the original format and in some resized formats. I have this method that takes an image as argument and returns an image with the specified size:

thumbnailmage = ResizeImage(originalImage, new Size(50,50));

The image returned is proportional to the original image with no side bigger than 50 pixels. E.g. if the originalImage has width 100 and height 200 the resized image has width 25 and height 50. This result is expected and wanted.

Now, to what I really want. I want to modify the ResizeImage method to return a squared thumbnail. I.e. in the originalImage example sizes above, the returned image should have width width and heigth both 50. Also, the image should not be stretched but padded with 12.5 pixels on the left and right side making the width 50. The image format I currently work with is gif with transparent background so the padded image parts should also be tansparent. Here is the code for ResizeImage, does anyone know how it could be modified?

  private static Image ResizeImage(Image imageToResize, Size size)
      int sourceWidth = imageToResize.Width;
      int sourceHeight = imageToResize.Height;


Dialogs and ViewModel - Using Tasks as a Pattern

The ViewModel/MVVM pattern continues to gain popularity, with a blog post showing up every so often, and with tweets and retweets popping up even more often :-). At the same time, there are some interesting topics beyond the core pattern that continue to fuel experimentation. A big one amongst those is how should applications use dialogs when using the view model pattern.

The crux of the problem is the desire to keep the view model independent of UI concerns, and ensure it can be tested in a standalone manner, but that often comes to odds when you want the view model to launch a dialog, and/or do some work after the dialog is closed.

Abstract Factory Design in C# , Vb.NET

Provide an interface for creating families of related or dependent objects without specifying their concrete classes

Prototype Design Pattern in C#. Vb.NET

Specify the kind of objects to create using a prototypical instance, and create new objects by copying this prototype

Adapter Design Pattern in C#, VB.NET

Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

Singleton Design Pattern in C#, Vb.NET

Ensure a class has only one instance and provide a global point of access to it.

Binding WPF Controls to an Entity Data Model

In this interview, programming writer, McLean Schofield, demonstrates how to bind WPF controls to an entity data model, using Visual Studio 2010 Beta 1. You can also learn more in the topic: Walkthrough: Binding WPF Controls to an Entity Data Model.
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend