This is a migrated thread and some comments may be shown as answers.

SelectedItems Binding

6 Answers 457 Views
GridView
This is a migrated thread and some comments may be shown as answers.
naresh
Top achievements
Rank 1
naresh asked on 27 Oct 2011, 09:09 AM
Hi,

I am facing small problem.That I want know selectedItems in radgridview.I am using MVVM pattern.In code behind file i am able to get the selectedItems.But in my viewmodel I am I am not getting.I had tried with following code samples.


<Style TargetType="{x:Type telerik:GridViewRow}">
<Setter Property="IsSelected" Value="{Binding Mode=OneWayToSource, Path=SelectedYN}"/>
</Style>
 and 
<telerik:RadGridView.ItemContainerStyle>
<Style TargetType="{x:Type telerik:GridViewRow}">
<Setter Property="IsSelected" Value="{Binding Mode=OneWayToSource, Path=SelectedYN}"/>
</Style>
</telerik:RadGridView.ItemContainerStyle> SelectedYN is a property in my viewModel.(Its updates the ItemsSource)
Please help me in this issue.

Thanks and Regards
Naresh Mesineni

6 Answers, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 01 Nov 2011, 10:07 AM
Hi Naresh,

You have pasted just the XAMl, not the ViewModel nor the way you read the state in code behind.
So having the XAML only I guess the first place to  check would be the Binding mode . I have noticed it is  OneWayToSource.

All the best,
Pavel Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
naresh
Top achievements
Rank 1
answered on 02 Nov 2011, 09:07 AM
Hi Pavlov,
Thanks for reply.Here I am pasting my viewmodel and code part.As I mentioned I am using MVVM pattern.
private string projectNumber;
public string ProjectNumber
{
get { return projectNumber; }
set
{
if (value != projectNumber)
{
projectNumber = value;
NotifyOfPropertyChange(() => ProjectNumber);
}
}
}
		
private string selectedYN;
public string SelectedYN
{
get { return selectedYN; }
set
{
if (value != selectedYN)
{
selectedYN= value;
NotifyOfPropertyChange(() => SelectedYN);
}
}
}
Here I am
accessing SelectedItems like this
var selectedItems = DataSourceItems.Where(x => x.SelectedYN).ToList();

DataSourceItems was gridview datasource.
in XAML I have tried following ways.Tried with all types of modes.
<Style TargetType="{x:Type telerik:GridViewRow}">
<Setter Property="IsSelected" Value="{Binding Mode=OneWayToSource, Path=SelectedYN}"/>
</Style>
 and 
<telerik:RadGridView.ItemContainerStyle>
<Style TargetType="{x:Type telerik:GridViewRow}">
<Setter Property="IsSelected" Value="{Binding Mode=OneWayToSource, Path=SelectedYN}"/>
</Style>
</telerik:RadGridView.ItemContainerStyle> Please help in this matter.If possible provide example. Thanks and Regards Naresh Mesineni
0
Dave
Top achievements
Rank 1
answered on 31 Jul 2014, 12:14 PM
Pavlov, or someone from Telerik
I am having this exact same problem. Is there a solution?

Dave
0
Dave
Top achievements
Rank 1
answered on 31 Jul 2014, 01:23 PM
In case this is of any help to anyone, I solved the problem. In my case it was caused by the fact that all our telerik styles are overridden by a complete set of styles that implement our company look-and-feel. So we don't use the telerik XAML, we use a set of BAML DLLs developed by our UX team.
Anyway, try this:

<Style x:Key="MyGridViewRowStyle" TargetType="{x:Type telerik:GridViewRow}" BasedOn="{StaticResource {x:Type telerik:GridViewRow}}">
     <Setter Property="IsSelected" Value="{Binding Mode=OneWayToSource, Path=SelectedYN}"/>
</Style>
<Style TargetType="{x:Type telerik:GridViewRow}" BasedOn="{StaticResource MyGridViewRowStyle}" />

I don't know if this is a workaround for a bug in WPF, or whether it is necessary for some good reason that I don't understand, but it worked for me.
0
Dave
Top achievements
Rank 1
answered on 31 Jul 2014, 01:28 PM
PS. On second thoughts I don't think the company style overrides have anything to do with it. It's just an oddity of WPF.
0
Dimitrina
Telerik team
answered on 01 Aug 2014, 12:35 PM
Hi,

You can check this github example on how to bind the SelectedItems to a collection from your view model
Although GitHub is a very well-known platform we saw a better and easier approach for reviewing our examples developing our brand new SDK Samples Browser. You can also use it to review the examples.

I hope it will work better for you. Let me know what your observations are.

Regards,
Didie
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
Tags
GridView
Asked by
naresh
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
naresh
Top achievements
Rank 1
Dave
Top achievements
Rank 1
Dimitrina
Telerik team
Share this question
or