This question is locked. New answers and comments are not allowed.
Hi,
I'm having difficulties in creating a button column in a RadGridView. The problem is that it seems like each button is not connected to any command in the viewmodel because nothing happens when clicking on the buttons.
I have followed several threads on this forum and the Telerik documentation but nothing has helped me. Does anyone have a clue what's wrong?
Regards, Clas
VIEW
VIEW CODEBEHIND
VIEWMODEL
I'm having difficulties in creating a button column in a RadGridView. The problem is that it seems like each button is not connected to any command in the viewmodel because nothing happens when clicking on the buttons.
I have followed several threads on this forum and the Telerik documentation but nothing has helped me. Does anyone have a clue what's wrong?
Regards, Clas
VIEW
<
UserControl
x:Class
=
"XYZ.Views.MonthReport.MonthReportView"
xmlns:telerik
=
"http://schemas.telerik.com/2008/xaml/presentation"
xmlns:local
=
"clr-namespace:XYZ.Views.MonthReport"
>
<
Grid
x:Name
=
"LayoutRoot"
MaxWidth
=
"1200"
>
<
Grid.RowDefinitions
>
<
RowDefinition
Height
=
"Auto"
/>
<
RowDefinition
Height
=
"Auto"
/>
<
RowDefinition
Height
=
"Auto"
/>
<
RowDefinition
Height
=
"*"
/>
</
Grid.RowDefinitions
>
<
telerik:RadGridView
x:Name
=
"MonthReportGrid"
Grid.Row
=
"2"
IsBusy
=
"{Binding IsLoading}"
ItemsSource
=
"{Binding SelectList}"
SelectedItem
=
"{Binding SelectedItem, Mode=TwoWay}"
>
<
telerik:RadGridView.Columns
>
<!-- Other columns have been left out clearer code -->
<
telerik:GridViewColumn
Header
=
"Mätarbyte"
>
<
telerik:GridViewColumn.CellTemplate
>
<
DataTemplate
>
<
telerik:RadButton
x:Name
=
"btnCounterChange"
Content
=
"Lägg till"
Command
=
"{Binding ShowDialogCommand}"
CommandParameter
=
"{Binding counterId}"
/>
</
DataTemplate
>
</
telerik:GridViewColumn.CellTemplate
>
</
telerik:GridViewColumn
>
</
telerik:RadGridView.Columns
>
</
telerik:RadGridView
>
<
StackPanel
Orientation
=
"Horizontal"
Grid.Row
=
"3"
Margin
=
"0,5"
>
<
telerik:RadButton
x:Name
=
"btnSaveMonthReport"
Content
=
"Spara inrapportering"
Command
=
"{Binding SaveMonthReportCommand}"
/>
</
StackPanel
>
</
Grid
>
</
UserControl
>
using
System;
using
System.Collections.Generic;
using
System.Windows.Data;
using
System.Linq;
using
System.Linq.Expressions;
using
System.Windows.Controls;
using
System.Windows;
using
Telerik.Windows.Controls;
using
Telerik.Windows.Controls.GridView;
namespace
XYZ.Views.MonthReport
{
public
partial
class
MonthReportView : UserControl, IMonthReportView
{
private
IEnumerable<GridViewColumn> columnSelectorSource =
null
;
public
IEnumerable<GridViewColumn> ColumnSelectorSource
{
get
{
return
columnSelectorSource; }
set
{ columnSelectorSource = value; }
}
/// <summary>
/// ViewModel attached to the View
/// </summary>
public
IMonthReportViewModel Model
{
get
{
return
this
.DataContext
as
IMonthReportViewModel; }
set
{
this
.DataContext = value; }
}
/// <summary>
/// Default constructor
/// </summary>
public
MonthReportView()
{
InitializeComponent();
}
}
VIEWMODEL
using
System;
namespace
XYZ.Views.MonthReport
{
public
class
MonthReportViewModel : ViewModel<IMonthReportView>, IMonthReportViewModel
{
/// <summary>
/// Event aggregator
/// </summary>
private
readonly
IEventAggregator eventAggregator;
/// <summary>
/// Service principal
/// </summary>
private
readonly
IMonthReportService dataService;
/// <summary>
/// data Controller
/// </summary>
private
readonly
IMonthReportController dataController;
/// <summary>
/// Unity container
/// </summary>
private
readonly
IUnityContainer container;
private
bool
isCounterChangeDialogOpen =
false
;
#endregion
public
IUnityContainer Container
{
get
{
return
container;
}
}
private
bool
isLoading;
public
bool
IsLoading
{
get
{
return
isLoading;
}
set
{
if
(isLoading != value)
{
isLoading = value;
Notify(() =>
this
.IsLoading);
}
}
}
private
MonthReportEntity _selectedItem;
public
MonthReportEntity SelectedItem
{
get
{
return
_selectedItem;
}
set
{
if
(_selectedItem != value)
{
_selectedItem = value;
Notify(() =>
this
.SelectedItem);
}
}
}
private
MonthReportEntityCollection _selectList;
public
MonthReportEntityCollection SelectList
{
get
{
return
_selectList;
}
set
{
if
(_selectList != value)
{
_selectList = value;
Notify(() =>
this
.SelectList);
}
}
}
//This one's not working
private
DelegateCommand _showDialogCommand;
public
DelegateCommand ShowDialogCommand
{
get
{
if
(_showDialogCommand ==
null
)
{
_showDialogCommand =
new
DelegateCommand((o) =>
{
OnShowDialog(o
as
object
);
});
}
return
_showDialogCommand;
}
set
{
_showDialogCommand = value;
Notify(() =>
this
.ShowDialogCommand);
}
}
//This one's working
private
DelegateCommand _saveMonthReportCommand;
public
DelegateCommand SaveMonthReportCommand
{
get
{
return
_saveMonthReportCommand;
}
set
{
_saveMonthReportCommand = value;
Notify(() =>
this
.SaveMonthReportCommand);
}
}
/// <summary>
/// Default constructor
/// </summary>
/// <param name="view"></param>
/// <param name="eventAggregator"></param>
/// <param name="dataService"></param>
/// <param name="dataController"></param>
/// <param name="container"></param>
public
MonthReportViewModel(IMonthReportView view,
IEventAggregator eventAggregator, IUnityContainer container, IMonthReportService dataService, IMonthReportController dataController)
{
this
.eventAggregator = eventAggregator;
this
.dataController = dataController;
this
.dataService = dataService;
this
.container = container;
this
.View = view;
this
.View.Model =
this
;
dataService.onGetMonthReportComplete +=
new
EventHandler<MonthReportEventArgs>(OnGetMonthReportComplete);
dataService.onSaveMonthReportComplete +=
new
EventHandler<MonthReportEventArgs>(OnSaveMonthReportComplete);
InitializeData();
InitializeCommands();
}
// Initializing methods
public
void
InitializeCommands()
{
this
.ShowDialogCommand =
new
DelegateCommand((o) =>
{
OnShowDialog(o
as
object
);
});
this
.SaveMonthReportCommand =
new
DelegateCommand(
this
.SaveMonthReport,
this
.CanSaveMonthReport);
}
public
void
InitializeData()
{
GetMonthReport();
}
//Methods for handling the dialog
//This method is never called. I have checked with breakpoints.
public
void
OnShowDialog(
object
args)
{
if
(!isCounterChangeDialogOpen)
{
if
((SelectedItem ==
null
) || ((
int
) args != SelectedItem.counterId))
{
SelectedItem = SelectList.GetEntityWithId((
int
)args);
}
var dialog =
this
.Container.Resolve<ICounterChangeDialogViewModel>();
dialog.ConfirmAction =
new
Action<ICounterChangeDialogViewModel,
bool
?>(
this
.OnConfirm);
CreateCounterChange();
dialog.SelectedMRE =
this
.SelectedItem;
dialog.View.Show();
isCounterChangeDialogOpen =
true
;
}
}
public
void
OnConfirm(ICounterChangeDialogViewModel sender,
bool
? confirmResult)
{
if
(confirmResult !=
null
)
{
if
((
bool
)confirmResult)
{
//Yet to be implemented
}
}
sender.Clean();
isCounterChangeDialogOpen =
false
;
}
//Methods for fetching the data via WCF
public
void
GetMonthReport()
{
this
.IsLoading =
true
;
dataService.GetMonthReport();
}
public
bool
CanSaveMonthReport()
{
return
(isCounterChangeDialogOpen) ?
false
:
true
;
}
public
void
SaveMonthReport()
{
this
.IsLoading =
true
;
dataService.SaveMonthReport(SelectList);
}
void
OnGetMonthReportComplete(
object
sender, MonthReportEventArgs e)
{
if
(!e.HasError)
this
.SelectList = e.MonthReportData;
else
{
this
.SelectList =
null
;
eventAggregator.GetEvent<ErrorEvent>().Publish(
new
ErrorEvent
{
Message = e.Error.ToString(),
ExceptionObj = e.Error
});
}
this
.IsLoading =
false
;
}
void
OnSaveMonthReportComplete(
object
sender, MonthReportEventArgs e)
{
if
(!e.HasError)
{
eventAggregator.GetEvent<MessageShowEvent>().Publish(
new
MessageShowEvent
{
MessageTitle =
"Bekräftelse"
,
Message =
"Månadsrapporteringen sparades."
});
}
else
{
this
.SelectList =
null
;
eventAggregator.GetEvent<ErrorEvent>().Publish(
new
ErrorEvent
{
Message = e.Error.ToString(),
ExceptionObj = e.Error
});
}
this
.IsLoading =
false
;
}
public
override
void
Clean()
{
base
.Clean();
}
}
}