SelectedItems Binding

7 posts, 0 answers
  1. naresh
    naresh avatar
    23 posts
    Member since:
    Jul 2012

    Posted 27 Oct 2011 Link to this post

    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
  2. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 01 Nov 2011 Link to this post

    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 >>

  3. naresh
    naresh avatar
    23 posts
    Member since:
    Jul 2012

    Posted 02 Nov 2011 Link to this post

    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
  4. Dave
    Dave avatar
    31 posts
    Member since:
    Apr 2014

    Posted 31 Jul 2014 in reply to naresh Link to this post

    Pavlov, or someone from Telerik
    I am having this exact same problem. Is there a solution?

    Dave
  5. Dave
    Dave avatar
    31 posts
    Member since:
    Apr 2014

    Posted 31 Jul 2014 in reply to Dave Link to this post

    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.
  6. Dave
    Dave avatar
    31 posts
    Member since:
    Apr 2014

    Posted 31 Jul 2014 in reply to Dave Link to this post

    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.
  7. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 01 Aug 2014 Link to this post

    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.
     
Back to Top