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


Top 5 Contributors of the Month
Kaviya Balasubramanian
Imran Ghani
Post New Web Links

Binding Command Inside DataGrid Using MVVM

Posted By: Asim Sajjad     Posted Date: June 19, 2011    Points: 2   Category :Silverlight
 
In this post I will show you how to bind the commands inside the data grid control using MVVM pattern. For this post I have continue my last post which is used to explain the data pager using MVVM. For this post I have added DelegateCommand class which is added in the Classes folder. I have only one command in the viewModel (with the name HomeViewModel and is located in the ViewModel folder). The view for the Home is located in the View folder. Here you can see that I have added the HomeViewModel as resource in the home view which you can see in the Image 1.


View Complete Post


More Related Resource Links

Binding DataGrid RowDetailTemplate Using MVVM

  
In this post I will show you how you can bind the RowDetailTemplate of the data grid using the MVVM pattern. Below are the image of the sample code here you can see the default page in Image 1 and when user click on any of the record then detail of the record is shown in the RowDetailTemplate which also consist of the data grid control which you can see in the Image 2.

Click.CommandParameter null when binding on Button inside DataGrid DataGridTemplateColumn

  
I am having trouble getting a Click.CommandParameter to pass through with a bound Click.Command. I have a DataGrid with a DataGridTemplateColumn that contains a couple of Buttons. Using a suggestion found on the forum (http://social.msdn.microsoft.com/Forums/en/wpf/thread/541024a9-3d7e-4c6a-b88e-9a7eb7ee83a9), I was able to get the Click.Command to bind to the right command, but when arriving in the corresponding method in my ViewModel, the parameter is null.  Here is my xaml for my DataGridTemplateColumn <data:DataGridTemplateColumn Header="Case">  <data:DataGridTemplateColumn.CellTemplate>   <DataTemplate >    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">     <Button Content="Open" Margin="6,0" command:Click.Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type data:DataGrid}}, Path=DataContext.OpenCaseCommand}" CommandParameter="{Binding}"  />     <Path Data="M1,1 L1,2" Stroke="#FFCCCCCC"/>     <Button Content="Rename" />    </StackPanel>   </DataTemplate>  </data:DataGridTemplateColumn.CellTemplate> </data:DataGridTemplateColumn> I used Snoop to see what was going on. Everything

Command inside WPF 4 DataGrid RowDetailsTemplate Not firing

  

Hi all,

I am using VS2010 WPF4 + Prism 2.2 to build my application. In oder to demo the issue, I built a very simple application here:

The Xaml of window (MainWindow.xaml), I defined a DataGrid and a button. I bind command of button inside row details to a DelegateCommand defined in ModelView, I also bind button (below the DataGrid) to another command.

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow"

Using MVVM and commands inside of a datatemplate, the command wont execute.

  
Inside of my main view i have a datatemplate showing off all the items in an observable collection. now inside of that i want to set commanding on a radio button.

outside the datatemplate this works
<Button Height="23" Name="btnUpdate" Command="{Binding UpdateCommand}"

Cutting Edge: Server-side ASP.NET Data Binding, Part 2: Customizing the DataGrid Control

  

How to get the combobox value inside Datagrid on Edit button click event

  
Hi     I have Datagrid template column like<DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn>DataGridTemplateColumn Header="Edit" x:Name="Edit" ><DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn.CellEditingTemplate></DataGridTemplateColumn>SelectionChanged="cmbDGTeam_SelectionChanged" IsTabStop="True" /></DataTemplate><ComboBox Height="22" IsEditable="True" IsReadOnly="True" ItemsSource="{Binding}" DisplayMemberPath="Title" SelectedValuePath="TeamID" x:Name="cmbDGTeam"  SelectedValue="{Binding Path=TeamID,Mode=TwoWay}"<DataGridTemplateColumn.CellEditingTemplate><DataTemplate><DataTemplate><TextBlock Text="{Binding Title}"/></DataTemplate></DataGridTemplateColumn.CellTemplate><DataGridTemplateColumn Header="Team" Width="75"><DataGridTemplateColumn.CellTemplate> and Edit button Template column <DataGridTemplateColumn Header="Edit" x:Name="Edit" > </DataGridTemplateColumn.CellTemplate></DataTemplate><Button Content="Edit" IsEnabled="False" x:Name="btnEdit" Click="btnEdit_Click" ></Button>&l

Data Binding a DataGrid to a list fetch the same data multiple times

  
Hi, I'm using DataGrid to show data through data binding. My data are provided in a list. The setup I have is the following: DataGrid "binded" to the default view of my list that implement IList<T> and IList interfaces (MyList<T> : IList<T>, IList, INotifyCollectionChanged)             CollectionViewSource.GetDefaultView(_myList);             // Set the data context             DataContext = view; T: is here my 'business object' that hold the data, called 'MyRow'. Then the columns of the DataGrid are bound to public properties of 'MyRow' object. MyRow class has a public property of type 'object' (public object data[int index]) and first column of datagrid is bound to myRow.data[0], second column is bound to myRow.data[1] and so on.   When the datagrid is displayed, databinding kick in and my data are showed in the control. The issue I'm having is that data fetching is very repetitive even for the same rows. For example for a datagrid with 20 visible rows, the first row is fetched multiple times even if the datagrid is not resized or moved. I put break point inside my list (MyList<T> into This[T] property (public T this[int index]) and i got multiple hits into the property for the same index. Fetc

Event Binding in MVVM

  
We are implementing the MVVM in the WPF forms that we are designing. For this we have designed the specific ViewModels for each WPF view. The WPF form has various controls such as button, combo box etc. We are registering the button click event and other events in the ViewModel constructor. Is there better way of handling this? Pls.guide

binding datetimecontrol,dropdown,textbox inside gridview and update each gridviewrow

  
i have a web part with datetimecontrol(sharepoint) inside a  gridview. when the page loads this this gridview will be loaded with dtc control and the selecteddate would be one of the columns in the splist.also gridview contaisn dropdowns, textboxes, when an update button cliked inside grid, i need to save the info in the splist.  currently i am not getting the value  bound with dtc control. i am getting blank date inside dtc. though in the  rowdatabound event i amcapturing the values due to some reason i am not able to view the value in the DTCcontrol. can anybody give me help me how to achieve this? any  help links/ sample code.. appreciate the help                         

Accessing Combobox inside WPF Datagrid Template Column

  
Hi, I am creating a WPF datagrid template column with combobox. The problem that I face now is, I am not able to bind a table to the combobox inside the WPF datagrid template column. Here is the code snippet. <dg:DataGrid x:Name="Students" AutoGenerateColumns="False" Loaded="OnLoad"> <dg:DataGrid.Columns> <dg:DataGridTextColumn Binding="{Binding Path=Name}" Header="Name" Width="75"/> <dg:DataGridTemplateColumn x:Name="Dept"> <dg:DataGridTemplateColumn.CellTemplate> <DataTemplate> <ComboBox x:Name="DeptId" ItemsSource="{Binding}" SelectionChanged="Dept_SelectionChanged" DisplayMemberPath="{Binding Path=DeptName}" SelectedValuePath="{Binding Path=DeptId}" SelectedItem="{Binding Path=Belongs}"/> </DataTemplate> </dg:DataGridTemplateColumn.CellTemplate> </dg:DataGridTemplateColumn> </dg:DataGrid.Columns> </dg:DataGrid> Please help me to find out the way to assign a dynamic source to the combobox through code behind file. Thanks in Advance!!! Best Regards, Subalakshmi Vijayarajan.

Command binding issue with using CommandReference and CommandGroup

  
I'm trying to write an app using the MVVM pattern but I'm running into an command binding issue. I'm trying to bind a button to multiple commands. To do this I'm using the CommandReference class (this is from the MVVM toolkit) and the CommandGroup class (I found this CodeProject). Here is a simplified version of my xaml: <Window.Resources> <c:CommandReference x:Key="TestCommand" Command="{Binding Path=TestCommand}"/> <c:CommandGroup x:Key="TestCommandGroup"> <c:CommandReference Command="{StaticResource TestCommand}"/> <c:CommandReference Command="{StaticResource TestCommand}"/> </c:CommandGroup> </Window.Resources> <Grid> <Button Content="Command Test" Command="{StaticResource TestCommandGroup}"/> </Grid> If I set the DataContext of the window to itself, this works just fine and the TestCommand will execute twice. The problem occurs when I want to follow the MVVM pattern and I set the DataContext of the window to a ViewModel. When I do this the button becomes disabled (I don't see any binding errors in the output window). I tried debugging this to see what was going on. If I set a breakpoint on the PropertyChangedCallback for the Command, I noticed the sequence this gets called is diff

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

delete button in datagrid in MVVM not working

  
i have a datagrid. i have added a delete button as data template. but on button click, the event is not firing .  <sdk:DataGrid AutoGenerateColumns="False"                       Height="94"                       HorizontalAlignment="Left"                       Margin="0,53,0,0"                       Name="dataGrid1" VerticalAlignment="Top" Width="560"                       ItemsSource="{Binding SelectedComponentInfo.FilterComponentCriteriaMapping}"                       SelectedItem="{Binding SelectedMappingInfo, Mode=TwoWay}" CanUserReorderColumns="True" CanUserResizeColumns="True" DataContext="{Binding}">    

delete button in datagrid in MVVM not working

  
hi all, i am using VS 2010 with silverlight 4.0. i have a datagrid. i have added a delete button as data template. but on button click, the event is not firing .  <sdk:DataGrid AutoGenerateColumns="False"                       Height="94"                       HorizontalAlignment="Left"                       Margin="0,53,0,0"                       Name="dataGrid1" VerticalAlignment="Top" Width="560"                       ItemsSource="{Binding SelectedComponentInfo.FilterComponentCriteriaMapping}"                       SelectedItem="{Binding SelectedMappingInfo, Mode=TwoWay}" CanUserReorderColumns="True" CanUserResizeColumns="True" DataCon

delete button in datagrid in MVVM not working

  
hi all, i am using VS 2010 with silverlight 4.0. i have a datagrid. i have added a delete button as data template. but on button click, the event is not firing .  <sdk:DataGrid AutoGenerateColumns="False"                       Height="94"                       HorizontalAlignment="Left"                       Margin="0,53,0,0"                       Name="dataGrid1" VerticalAlignment="Top" Width="560"                       ItemsSource="{Binding SelectedComponentInfo.FilterComponentCriteriaMapping}"                       SelectedItem="{Binding SelectedMappingInfo, Mode=TwoWay}" CanUserReorderColumns="True" CanUserResizeColumns="True" DataCon

TwoWay Binding Datagrid To WCF Service?

  
Hello, i tried to bind a DataGrid in TwoWay Mode to a WCF Service. The Data get displayed but changes in DataGrid dont affect the Database, from which the WCF Service is getting the data.   I hope someone can help me... Thank you.     My Szenario: I have a WCF Service which offers exactly one Function which return a custom class. The custom class inherits from INotifyPropertyChanged and has only one DataMember, an ObservableList with my DB Entitys. //Service.svc.cs static testdatenEntities _context = null; private static testdatenEntities Get_context() { if (_context == null) { _context = new testdatenEntities(); } return _context; } public customData GetDataFromADO() { var _context = Get_context(); var result = (from lief in _context.LIEF_INFO.AsQueryable() orderby lief.LNR select lief).Skip(50).Take(50); ObservableCollection<LIEF_INFO> _ocol = new ObservableCollection<LIEF_INFO>(result); customData customdata = new customData() { List = _ocol }; return customdata; } //IService.cs [DataContract] public class customData : INotifyPropertyChanged { ObservableCollection<LIEF_INFO> _ocol = new ObservableCollection<LIEF_INFO>(); [DataMember] public ObservableCollection<LIEF_INFO> List {

Binding DataGrid to empty DataTable

  
Hi, I'm very much new to WPF and am finding it a bit tricky to find my way in the mass of (not always great) documentation. Right now I'm struggling to get the grid to display it's UI when bound to an empty datatable. In my XAML: <DataGrid Name="grid" ItemsSource="{Binding}" />  In my code-behind (MainWindow constructor, following InitializeComponent): var dt = new DataTable(); dt.Columns.Add("col1"); dt.Columns.Add("col2"); //var r = dt.Rows.Add("toto", "tata"); grid.DataContext = dt; If I comment out the line that adds a row to the table, all is well -- the grid generates columns and displays UI. But if the table is empty, the grid renders a thin horizontal white rectangle with no column names and it doesn't seem possible to interact with this at all. As it happens, I want the grid to be empty by default, so I need to somehow get the grid to play along. If anyone knows why this happens and what to do to solve it, I'd much appreciate the help!
Categories: 
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