I am usingRadGridView.
<
telerik:RadGridView
x:Name
=
"view1"
Width
=
"1025"
IsReadOnly
=
"True"
ValidatesOnDataErrors
=
"None"
AutoGenerateColumns
=
"False"
IsFilteringAllowed
=
"False"
ShowGroupPanel
=
"False"
ShowColumnFooters
=
"False"
CanUserResizeColumns
=
"False"
CanUserFreezeColumns
=
"False"
SelectionMode
=
"Single"
CanUserReorderColumns
=
"False"
CanUserSortColumns
=
"False"
CanUserInsertRows
=
"False"
CanUserDeleteRows
=
"False"
CanUserSelect
=
"False"
RowIndicatorVisibility
=
"Collapsed"
Height
=
"265"
ItemsSource
=
"{Binding MyInformation}"
>
<
telerik:RadGridView.Columns
>
I want to raise an event in ModelView class if I select a row. How to do it?
8 Answers, 1 is accepted
You could call SelectionChanged on the RadGridView, and in the code behind of the controller invoke the event on your view model as appropriate.
I've also put a button in my RadGridViews before that invokes a command from my view models if that helps you at all:
XAML:
<
telerik:GridViewColumn
>
<
telerik:GridViewColumn.CellTemplate
>
<
DataTemplate
>
<
telerik:RadButton
Content
=
"View Chart"
Command
=
"{Binding OpenRecordChartCommand, Source={StaticResource viewModel}, Mode=OneWay}"
CommandParameter
=
"{Binding}"
/>
</
DataTemplate
>
</
telerik:GridViewColumn.CellTemplate
>
</
telerik:GridViewColumn
>
C#:
public
class
MyViewModel
{
#region "Commands"
public
ICommand OpenRecordChartCommand
{
get
;
set
;
}
private
void
OnOpenRecordChartCommandExecuted(
object
obj)
{
var record = obj
as
wcfNamespace.RecordType;
if
(record !=
null
)
{
// Update the selected tracking record to this to start an async call back to update the updated tracking record used for the chart.
this
.reloadRecordFromDb(record);
// Create a RadWindow referencing the tracking record with a chart.
RadWindow radWindow =
new
RadWindow()
{
Header =
this
.GetHeaderForRecord(record),
Content =
new
MyRecordChartControl(record),
Opacity = 1.0,
WindowStartupLocation = Telerik.Windows.Controls.WindowStartupLocation.CenterOwner
};
// Show the RadWindow.
radWindow.Show();
}
}
#endregion "Commands"
#region "Constructor"
public
MyViewModel()
{
if
(!IsDesignTime)
{
this
.OpenRecordChartCommand =
new
DelegateCommand(OnOpenRecordChartCommandExecuted);
}
}
#endregion "Constructor"
}
Thanks,
Ravin
I want to do it in code behind. I have a mouse down event when I click a row with mouse.
01.
private
void
radGridView_RowLoaded(
object
sender, RowLoadedEventArgs e)
02.
{
03.
var row = e.Row
as
GridViewRow;
04.
if
(row !=
null
)
05.
{
06.
row.IsCurrent =
true
;
07.
row.AddHandler(GridViewRow.MouseLeftButtonDownEvent,
08.
new
MouseButtonEventHandler(GridViewRow_MouseLeftButtonDown),
true
);
09.
}
10.
}
11.
12.
private
void
GridViewRow_MouseLeftButtonDown(
object
sender, MouseButtonEventArgs e)
13.
{
14.
// change row color????
15.
}
Thanks for help.
This approach may work, can't really test it myself, as I have other behavior in my grid which is preventing the event from ever firing:
private
void
radGridView_RowLoaded(
object
sender, RowLoadedEventArgs e)
{
var row = e.Row
as
GridViewRow;
if
(row !=
null
)
{
//row.IsCurrent = true;
row.IsHitTestVisible =
true
;
row.MouseLeftButtonDown += (handlerSender, handlerArgs) =>
{
row.Background =
new
SolidColorBrush() { Color = Colors.Yellow };
};
}
}
Let me know if that works for you.
Thanks,
Ravin
No. It is not working. The code doesn't reach the inside after the mouse left button down.
1.
row.MouseLeftButtonDown += (
object
s, MouseButtonEventArgs me) =>
2.
{
3.
row.Background =
new
SolidColorBrush() { Color = Colors.Green };
4.
};
I think that we need to pass additional data (row) to the method.
private
void
GridViewRow_MouseLeftButtonDown(
object
sender, MouseButtonEventArgs e)
{
// need row object
}
There is a link for that.
But I am not sure how to apply it.
Regarding the original question in the thread - How to raise an event in ModelView class If I select a row - Please check the following article from our documentation - EventToCommandBehavior.
However, as far as I understand, your requirement is to change the row`s background when the row is selected. The best way to achieve this is to predefine the template of the GridViewRow. You need to modify the Border element named "Background_Selected" in the VisualStates with the desired color. Please, check the following article - Styling Rows.
You can also achieve the behavior through Style Triggers:
<
Style
TargetType
=
"telerik:GridViewCell"
BasedOn
=
"{StaticResource GridViewCellStyle}"
>
<
Style.Triggers
>
<
Trigger
Property
=
"IsSelected"
Value
=
"True"
>
<
Setter
Property
=
"Background"
Value
=
"Green"
/>
</
Trigger
>
</
Style.Triggers
>
</
Style
>
The recommended approach is modifying the ControlTemplate, though.
Please update the thread if you have some difficulties.
Regards,
Stefan Nenchev
Telerik by Progress
Error: The attachable property 'Triggers' was not found in type 'Style'.
My application is Silverlight. Microsoft said it didn't support it.
Any other options?
Please excuse me for the misleading information regarding the Triggers as they are not supported in Silverlight.
Did you try the other option from my previous reply? I am here providing a project with the desired functionality. Please review it and consider such approach at your end as well.
Regards,
Stefan Nenchev
Telerik by Progress