Scenario 1:- When I select a Row and then click Edit /Deactivate in GridView , its working fine as Selected item is not null but if I don’t select any of the rows and try
to Edit then I will get error. On selecting the data row when edit button of particular row is clicked so that selected item is not null.
I am using MVVM Pattern and I am beginner in Silverlight. Is there any way to handle this scenario. Please find see my code for your reference.Help needed.Thanks in advance.
My Xaml :
<UserControl x:Class="MVVM_Sample.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:ViewModel="clr-namespace:MVVM_Sample.ViewModel"
xmlns:telerikGrid="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
>
<UserControl.Resources>
<ViewModel:FormModel x:Key="FormModel"/>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" DataContext="{StaticResource FormModel}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Grid.Row="0">
<Button Command="{Binding OpenFormCommand}" Content="Add Form" Width="100" Margin="4"></Button>
<Button Command="{Binding SortOrderCommand}" Content="Sort Order" Width="100" Margin="4"></Button>
</StackPanel>
<telerik:RadGridView IsReadOnly="True" AutoGenerateColumns="False" SelectedItem="{Binding SelectedForm,Mode=TwoWay}"
Grid.Row="1" HorizontalAlignment="Left" Name="radGridView1"
ItemsSource="{Binding Context.Forms}" VerticalAlignment="Top" Width="1000" >
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn Header="Form Name" DataMemberBinding="{Binding Name}" />
<telerik:GridViewDataColumn Header="Descritpion" DataMemberBinding="{Binding Description}" />
<telerik:GridViewDataColumn Header="Active" DataMemberBinding="{Binding Active}" />
<telerik:GridViewDataColumn Header="Sort Order" DataMemberBinding="{Binding SortOrder}" />
<telerik:GridViewHyperlinkColumn Header="Sort Order" DataMemberBinding="{Binding SortOrder}" />
<telerik:GridViewColumn Header="Edit">
<telerik:GridViewColumn.CellTemplate>
<DataTemplate>
<Button Command="{Binding EditCommand, Source={StaticResource FormModel}}" CommandParameter="{Binding SelectedForm, Source=
{StaticResource FormModel}}" Content="Edit" Width="100" Margin="4"></Button>
</DataTemplate>
</telerik:GridViewColumn.CellTemplate>
</telerik:GridViewColumn>
<telerik:GridViewColumn Header="Actions">
<telerik:GridViewColumn.CellTemplate>
<DataTemplate>
<Button Command="{Binding DeActivateCommand, Source={StaticResource FormModel}}" CommandParameter="{Binding SelectedForm,
Source={StaticResource FormModel}}">DeActivate</Button>
</DataTemplate>
</telerik:GridViewColumn.CellTemplate>
</telerik:GridViewColumn>
</telerik:RadGridView.Columns>
</telerik:RadGridView>
</Grid>
</UserControl>
My ViewModel :
using System;
using System.Net;
using System.Windows;
//using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using MVVM_Sample.Web;
using System.Collections.Generic;
using MVVM_Sample.Web.DataModel;
using System.ComponentModel;
using System.ServiceModel.DomainServices.Client;
using Telerik.Windows.Controls;
using MVVM_Sample.Views;
namespace MVVM_Sample.ViewModel
{
public class FormModel : INotifyPropertyChanged
{
DataContext ctx =null;
bool isLoading = false;
public event EventHandler SelectedFormchanged;
public DataContext Context
{
get { return this.ctx; }
set
{
this.ctx = value;
this.RaisePropertyChanged("Context");
}
}
private System.Windows.Controls.ChildWindow UCform;
public System.Windows.Controls.ChildWindow UCForm
{
get { return UCform; }
set { UCform = value; }
}
private Form selectedForm;
public Form SelectedForm
{
get { return selectedForm; }
set
{
selectedForm = value;
RaisePropertyChanged("SelectedForm");
if (SelectedFormchanged != null)
SelectedFormchanged (this, new EventArgs());
}
}
private Form newForm;
public Form NewForm
{
get { return newForm; }
set
{
newForm = value;
}
}
public Form CreateNewForm()
{
Form tempForm = new Form
{
Id = 2,
Abbreviation = "xcvxc",
Name = "",
Active = 1,
Description = "",
SortOrder = 1,
xCreated = DateTime.Now,
xCreatedUserId = "1",
xUpdated = DateTime.Now,
xUpdatedUserId = "1",
};
return tempForm;
}
public ICommand OpenFormCommand { get; set; }
public ICommand AddCommand { get; set; }
public ICommand DeActivateCommand { get; set; }
public ICommand SortOrderCommand { get; set; }
public ICommand EditCommand { get; set; }
public ICommand CancelCommand { get; set; }
public FormModel()
{
ctx = new DataContext();
LoadForms();
AddCommand = new DelegateCommand(AddForm);
CancelCommand = new DelegateCommand(CancelForm);
OpenFormCommand = new DelegateCommand(OpenFormWindow);
EditCommand = new DelegateCommand(EditForm);
DeActivateCommand = new DelegateCommand(this.DeActivateForm_Execute, this.DeActivateForm_CanExecute);
SelectedFormchanged += new EventHandler(FormModel_SelectedFormchanged);
}
void FormModel_SelectedFormchanged(object sender, EventArgs e)
{
}
private void EditForm(object parameter)
{
this.NewForm = parameter as Form;
this.UCForm = new FormWindow();
this.UCForm.DataContext = this;
this.UCform.Show();
}
private void OpenFormWindow(object parameter)
{
this.UCForm = new FormWindow();
this.UCForm.DataContext = this;
this.UCForm.Loaded += new RoutedEventHandler(UCForm_Loaded);
this.UCform.Show();
}
void UCForm_Loaded(object sender, RoutedEventArgs e)
{
this.NewForm = CreateNewForm();
}
private void CancelForm(object parameter)
{
this.UCform.Close();
}
private void DeActivateForm_Execute(object parameter)
{
ctx.Forms.Detach(parameter as Form);
ctx.SubmitChanges();
}
private bool DeActivateForm_CanExecute(object parameter)
{
return true;
}
private void AddForm(object parameter)
{
ctx.Forms.Add(this.NewForm);
ctx.SubmitChanges();
this.UCform.Close();
}
public void LoadForms()
{
LoadOperation<Form> lo = ctx.Load<Form>(ctx.GetFormsQuery());
//isLoading = true;
// ctx.Load(ctx.GetFormsQuery(), lo => { isLoading = false; }, null);
}
void lo_Completed(object sender, EventArgs e)
{
LoadOperation lo = sender as LoadOperation;
if (lo.HasError)
{
MessageBox.Show(lo.Error.Message);
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
}