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

MVVM Grid Binding how to handle Parameters

1 Answer 42 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Daniel
Top achievements
Rank 1
Daniel asked on 03 Feb 2014, 04:55 PM
Hello I've a RadGridView that is placed withing a RadWindow and wrapped within an UserControl.

The RadWindow will be initialized by an ContextMenu of a parent RadGridView (selected item) please see the code snippets below.

Code from the ContextMenu to open a new RadWindow:

var tmpItem = row.Item as ItemModel;
.....
case "ViewRelatedItems":
         if ( tmpItem != null )
         {
             RadWindow radWindow = new RadWindow();
             radWindow.Width = 510;
             radWindow.Height = 350;
  
             UserControlRelatedItemsEntries ucTmp = new UserControlRelatedItemsEntries(tmpItem.ID, tmpItem.Type);
               
             radWindow.Content = ucTmp;
             radWindow.Owner = Application.Current.MainWindow;
             radWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
             radWindow.ResizeMode = ResizeMode.NoResize;
             radWindow.Header = "Entries:"; //TODO
             radWindow.Show();
         }
         break;


XAML of the UserControl:
<UserControl x:Class="TEST..UserControlViewEntries"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:sys="clr-namespace:System;assembly=mscorlib"
             xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
             xmlns:viewModel="clr-namespace:test.ViewModels;assembly=Test.ViewModels"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="500">
    <UserControl.DataContext>
        <viewModel:MyViewModel />
    </UserControl.DataContext>
    <Grid>
         
        <telerik:RadGridView x:Name="radGridEntries" Margin="0,30,0,0"
                             ItemsSource="{Binding Entries}"
                             AutoGenerateColumns="False" DataLoadMode="Asynchronous">
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding UserName}" Header="User"/>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Date}" Header="Date"/>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding XDoneAsString}" Header="XDone"/>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Description}" Header="Description"/>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Type}" Header="Type"/>
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>
    </Grid>
</UserControl>

Code of the ViewModel
public class MyViewModel : ObservableObjectBase
    {
        #region Properties
        public int SelectedItemID { get; set; }
        public int SelectedItemTypeID { get; set; }
 
        private ObservableCollection<XModel> _items;
 
        public ObservableCollection<XModel> Entries
        {
            get
            {
                if ( _items == null )
                {
                    _items = new ObservableCollection<XModel>();
 
                    var tmpResults = DataService.GetItems( this.SelectedItemID, this.SelectedItemTypeID);
                    tmpResults.ForEach( i => _items.Add( i ) );
                }
                return _items;
            }
        }
        #endregion
 
        #region Methods
         
        #endregion
 
        #region Constructors
        public MyViewModel ()
        { }
        #endregion
    }

I come here not further...

How can i put the two required values ​​SelectedItemID and SelectedItemTypeID to the ViewModel, so that they can be used when the DataBinding request the the Entries?

I'll hold this 2 values inside the ContextMenu "tmpItem" (e.g. tmpItem.ID and tmpItem.Type) but i've no idea how i could put that throuth the UserControl and ViewModel.

I think I am using the wrong approach... hopefully someone could give me an idea how to solve this issue. 

Any help on this would be very welcome!

Kind regards
Daniel


1 Answer, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 06 Feb 2014, 03:46 PM
Hello,

You could bind the SelectedItem of RadGridView to the SelectedItemID in your ViewModel. That way in the ViewModel you will always have the currently selected item. I am not sure what is the purpose of SelectedItemTypeID and how it is used with RadGridView.

You can also check our online documentation on using RadContextMenu with a RadGridView.

Regards,
Didie
Telerik

Check out the new Telerik Platform - the only modular platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native apps. Register for the free online keynote and webinar to learn more about the Platform on Wednesday, February 12, 2014 at 11:00 a.m. ET (8:00 a.m. PT).

Tags
GridView
Asked by
Daniel
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Share this question
or