How can i fire the event of a button if its inside the RowDetailsTemplate of RadGridView?
Currently, im using a RelayCommand to call the event but its not firing.
Any ideas?
XAML
VIEWMODEL
I saw on some forums that i should define ViewModel as a resource in xaml but this line returns error (Cannot create an instance of RecipeListViewModel)
Then add a Source to the Command
Currently, im using a RelayCommand to call the event but its not firing.
Any ideas?
XAML
<
telerik:RadGridView.RowDetailsTemplate
>
<
DataTemplate
>
<
Grid
>
<
Grid.RowDefinitions
>
<
RowDefinition
/>
<
RowDefinition
/>
</
Grid.RowDefinitions
>
<
telerik:RadTabControl
HorizontalAlignment
=
"Left"
Height
=
"Auto"
Margin
=
"15"
VerticalAlignment
=
"Top"
Width
=
"Auto"
Background
=
"{StaticResource shinywhite_fill}"
Style
=
"{StaticResource SVG_RadTabControlStyle}"
BorderBrush
=
"#FFC2C2C2"
>
<
telerik:RadTabItem
Header
=
"Details"
Height
=
"30"
Style
=
"{StaticResource SVG_RadTabItemStyle}"
Background
=
"{x:Null}"
BorderBrush
=
"{x:Null}"
Margin
=
"0,0,2,0"
FontFamily
=
"/Recipe;component/Fonts/Fonts.zip#Myriad Web Pro"
FontSize
=
"12"
FontWeight
=
"Bold"
Foreground
=
"{x:Null}"
>
<
my:DetailsView
NewList
=
"False"
/>
</
telerik:RadTabItem
>
<
telerik:RadTabItem
Header
=
"Keywords"
Height
=
"30"
Style
=
"{StaticResource SVG_RadTabItemStyle}"
Background
=
"{x:Null}"
BorderBrush
=
"{x:Null}"
Margin
=
"0,0,2,0"
FontFamily
=
"/Recipe;component/Fonts/Fonts.zip#Myriad Web Pro"
FontSize
=
"12"
FontWeight
=
"Bold"
>
<
my:KeywordsView
NewList
=
"False"
/>
</
telerik:RadTabItem
>
<
telerik:RadTabItem
Header
=
"Picture"
Height
=
"30"
Style
=
"{StaticResource SVG_RadTabItemStyle}"
Background
=
"{x:Null}"
BorderBrush
=
"{x:Null}"
Margin
=
"0,0,2,0"
FontFamily
=
"/Recipe;component/Fonts/Fonts.zip#Myriad Web Pro"
FontSize
=
"12"
FontWeight
=
"Bold"
Foreground
=
"{x:Null}"
>
<
my:PictureView
NewList
=
"False"
/>
</
telerik:RadTabItem
>
<
telerik:RadTabItem
Header
=
"Ingredients"
Height
=
"30"
Style
=
"{StaticResource SVG_RadTabItemStyle}"
Background
=
"{x:Null}"
BorderBrush
=
"{x:Null}"
Margin
=
"0,0,2,0"
FontFamily
=
"/Recipe;component/Fonts/Fonts.zip#Myriad Web Pro"
FontSize
=
"12"
FontWeight
=
"Bold"
>
<
my:IngredientsView
NewList
=
"False"
/>
</
telerik:RadTabItem
>
<
telerik:RadTabItem
Header
=
"Calculation"
Height
=
"30"
Style
=
"{StaticResource SVG_RadTabItemStyle}"
Background
=
"{x:Null}"
BorderBrush
=
"{x:Null}"
Margin
=
"0,0,2,0"
FontFamily
=
"/Recipe;component/Fonts/Fonts.zip#Myriad Web Pro"
FontSize
=
"12"
FontWeight
=
"Bold"
>
<
my:CalculationView
NewList
=
"False"
/>
</
telerik:RadTabItem
>
<
telerik:RadTabItem
Header
=
"Procedure"
Height
=
"30"
Style
=
"{StaticResource SVG_RadTabItemStyle}"
Background
=
"{x:Null}"
BorderBrush
=
"{x:Null}"
Margin
=
"0,0,2,0"
FontFamily
=
"/Recipe;component/Fonts/Fonts.zip#Myriad Web Pro"
FontSize
=
"12"
FontWeight
=
"Bold"
>
<
my:ProcedureView
NewList
=
"False"
/>
</
telerik:RadTabItem
>
<
telerik:RadTabItem
Header
=
"HACCP"
Height
=
"30"
Style
=
"{StaticResource SVG_RadTabItemStyle}"
Background
=
"{x:Null}"
BorderBrush
=
"{x:Null}"
Margin
=
"0,0,2,0"
FontFamily
=
"/Recipe;component/Fonts/Fonts.zip#Myriad Web Pro"
FontSize
=
"12"
FontWeight
=
"Bold"
>
<
my:HACCPView
NewList
=
"False"
/>
</
telerik:RadTabItem
>
<
telerik:RadTabItem
Header
=
"Translation"
Height
=
"30"
Style
=
"{StaticResource SVG_RadTabItemStyle}"
Background
=
"{x:Null}"
BorderBrush
=
"{x:Null}"
Margin
=
"0,0,2,0"
FontFamily
=
"/Recipe;component/Fonts/Fonts.zip#Myriad Web Pro"
FontSize
=
"12"
FontWeight
=
"Bold"
>
<
my:TranslationView
NewList
=
"False"
/>
</
telerik:RadTabItem
>
</
telerik:RadTabControl
>
<
Button
Content
=
"Save Changes"
Grid.Row
=
"1"
Name
=
"btnSave"
Command
=
"{Binding Path=SaveDetailsCommand}"
Height
=
"25"
Margin
=
"5"
Style
=
"{StaticResource SVG_Button}"
HorizontalAlignment
=
"Right"
VerticalAlignment
=
"Bottom"
Foreground
=
"{x:Null}"
BorderBrush
=
"{x:Null}"
Background
=
"{x:Null}"
Cursor
=
"Hand"
/>
</
Grid
>
</
DataTemplate
>
</
telerik:RadGridView.RowDetailsTemplate
>
VIEWMODEL
Namespace
ViewModels
Public
Class
RecipeListViewModel
Inherits
NotificationObject
Public
WithEvents
webService
As
New
IwcfServiceClient
Private
m_dataService
As
IwcfServiceClient
Public
Sub
New
(
ByVal
intCodeSetPrice
As
Integer
,
ByVal
intCodeType
As
Integer
,
ByVal
intCodeUser
As
Integer
)
PageSize = 100
m_dataService =
New
IwcfServiceClient
IsBusy =
True
AddHandler
m_dataService.GetRecipeListCompleted,
AddressOf
webService_GetRecipeListCompleted
m_dataService.GetRecipeListAsync(intCodeSetPrice, intCodeType, intCodeUser)
' Initialize the commands.
SaveDetailsCommand =
New
RelayCommand(
AddressOf
SaveRecipeDetails)
CancelDetailsCommand =
New
RelayCommand(
AddressOf
CancelRecipeDetails)
End
Sub
Public
Sub
New
()
m_dataService =
New
IwcfServiceClient
AddHandler
m_dataService.GetUsersListCompleted,
AddressOf
webService_GetUsersListCompleted
m_dataService.GetUsersListAsync()
End
Sub
#Region "Property"
Private
m_usersList
As
ObservableCollection(Of UsersListDataItem)
Public
Property
UsersList()
As
ObservableCollection(Of UsersListDataItem)
Get
Return
m_usersList
End
Get
Private
Set
(
ByVal
value
As
ObservableCollection(Of UsersListDataItem))
m_usersList = value
Me
.RaisePropertyChanged(
"UsersList"
)
End
Set
End
Property
Private
m_recipeList
As
ObservableCollection(Of RecipeListDataItem)
Private
_items
As
PagedCollectionView =
Nothing
Public
ReadOnly
Property
RecipeList()
As
PagedCollectionView
Get
'If _items Is Nothing Then
' _items = New PagedCollectionView(m_recipeList)
'End If
Return
_items
Me
.RaisePropertyChanged(
"RecipeList"
)
End
Get
End
Property
Private
m_isBusy
As
Boolean
Public
Property
IsBusy()
As
Boolean
Get
Return
m_isBusy
End
Get
Private
Set
(
ByVal
value
As
Boolean
)
m_isBusy = value
Me
.RaisePropertyChanged(
"IsBusy"
)
End
Set
End
Property
Private
m_isEditMode
As
Visibility
Public
Property
IsEditMode()
As
Visibility
Get
Return
m_isEditMode
End
Get
Set
(
ByVal
value
As
Visibility)
m_isEditMode = value
Me
.RaisePropertyChanged(
"IsEditMode"
)
End
Set
End
Property
Private
_PageSize
As
Integer
Public
Property
PageSize
As
Integer
Get
Return
_PageSize
End
Get
Set
(
ByVal
value
As
Integer
)
_PageSize = value
Me
.RaisePropertyChanged(
"PageSize"
)
End
Set
End
Property
#End Region
#Region "Commands"
Private
m_saveDetailsCommand
As
ICommand
Public
Property
SaveDetailsCommand()
As
ICommand
Get
Return
m_saveDetailsCommand
End
Get
Set
(
ByVal
value
As
ICommand)
m_saveDetailsCommand = value
Me
.RaisePropertyChanged(
"SaveDetailsCommand"
)
End
Set
End
Property
Private
m_canceDetailsCommand
As
ICommand
Public
Property
CancelDetailsCommand()
As
ICommand
Get
Return
m_canceDetailsCommand
End
Get
Set
(
ByVal
value
As
ICommand)
m_canceDetailsCommand = value
Me
.RaisePropertyChanged(
"CancelDetailsCommand"
)
End
Set
End
Property
Public
Sub
SaveRecipeDetails()
'NOT FIRING ON THIS SUB
End
Sub
Public
Sub
CancelRecipeDetails()
'NOT FIRING ON THIS SUB
End
Sub
#End Region
#Region "Webservice"
Private
Sub
webService_GetRecipeListCompleted(
ByVal
sender
As
Object
,
ByVal
e
As
CalcmenuService.GetRecipeListCompletedEventArgs)
Handles
webService.GetRecipeListCompleted
If
Not
e.Result
Is
Nothing
And
e.Result.Count <> 0
Then
m_recipeList = e.Result
_items =
New
PagedCollectionView(m_recipeList)
Me
.RaisePropertyChanged(
"RecipeList"
)
IsBusy =
False
End
If
End
Sub
Private
Sub
webService_GetUsersListCompleted(
ByVal
sender
As
Object
,
ByVal
e
As
CalcmenuService.GetUsersListCompletedEventArgs)
Handles
webService.GetUsersListCompleted
If
Not
e.Result
Is
Nothing
Then
UsersList = e.Result
Me
.RaisePropertyChanged(
"UsersList"
)
End
If
End
Sub
Private
Sub
webService_SaveMerchandiseDetailsCompleted(
ByVal
sender
As
Object
,
ByVal
e
As
CalcmenuService.SaveMerchandiseDetailsCompletedEventArgs)
Handles
webService.SaveMerchandiseDetailsCompleted
IsEditMode = Visibility.Collapsed
End
Sub
#End Region
End
Class
Public
Class
RelayCommand
Implements
ICommand
ReadOnly
_execute
As
Action(Of
Object
)
ReadOnly
_canExecute
As
Predicate(Of
Object
)
Private
_action
As
System.Action
Public
Sub
New
(
ByVal
execute
As
Action(Of
Object
))
Me
.
New
(execute,
Nothing
)
End
Sub
Public
Sub
New
(
ByVal
execute
As
Action(Of
Object
),
ByVal
canExecute
As
Predicate(Of
Object
))
If
execute
Is
Nothing
Then
Throw
New
ArgumentNullException(
"execute"
)
End
If
_execute = execute
_canExecute = canExecute
End
Sub
Public
Function
CanExecute(
ByVal
parameter
As
Object
)
As
Boolean
Implements
System.Windows.Input.ICommand.CanExecute
Return
If
(_canExecute
Is
Nothing
,
True
, _canExecute(parameter))
End
Function
Public
Event
CanExecuteChanged(
ByVal
sender
As
Object
,
ByVal
e
As
System.EventArgs)
Implements
System.Windows.Input.ICommand.CanExecuteChanged
Public
Sub
Execute(
ByVal
parameter
As
Object
)
Implements
System.Windows.Input.ICommand.Execute
_execute(parameter)
End
Sub
End
Class
End
Namespace
I saw on some forums that i should define ViewModel as a resource in xaml but this line returns error (Cannot create an instance of RecipeListViewModel)
<UserControl.Resources>
<viewmodel:RecipeListViewModel x:Key="CommandViewModel" />
</UserControl.Resources>
Then add a Source to the Command
Command
="{Binding Path=SaveDetailsCommand, Source={StaticResource CommandViewModel}}"
So my work around is to call the event from code-behind when the gridview was expanded. But im not sure if this solution is good enough. I want to learn on how to use Icommand on Silverlight as i know i can use this function later on.
Private
Sub
RadGridItems_LoadingRowDetails(
ByVal
sender
As
Object
,
ByVal
e
As
Telerik.Windows.Controls.GridView.GridViewRowDetailsEventArgs)
Handles
RadGridItems.LoadingRowDetails
Dim
btnSave
As
Button
btnSave =
DirectCast
(e.DetailsElement.FindName(
"btnSave"
), Button)
AddHandler
btnSave.Click,
AddressOf
SaveChanges
End
Sub
Private
Sub
SaveChanges()
Dim
viewModel
As
RecipeListViewModel =
New
RecipeListViewModel()
viewModel.SaveRecipeDetails()
End
Sub