locked
DataGrid column button Click navigate to another page using (MVVM) RRS feed

  • Question

  • Hello,

    I have a Silverlight datagrid in which one column is button, when user clicks on the button i have to store the information in a session that is through WCF service (Session is present on server end) and navigate to next page, access the session and display all the text boxes etc. I have to use MVVM pattern for this. 

    Tuesday, October 25, 2011 5:40 PM

All replies

  • Hi Nchanda,

    If you want to use paging on DataGrid using MVVM, you can refer here: http://www.codeproject.com/KB/silverlight/RIADataGrid.aspx

    If you want to use Session, you will have two things to do before this. As MarkMonster said in this post:

    "1.The WCF service needs to support Sessions. That's something you need to configure first. http://msdn.microsoft.com/en-us/library/ms733040.aspx

    2.It's important to make sure your Silverlight application is using the right Session. It's a little bit dangerous. It's possible that sessions are shared between different Silverlight applications when not designed carefully."

    navigate to next page, access the session and display all the text boxes etc.

    Though, could you please clarify what do you want to do with session?

    Best Regards,

    Thursday, October 27, 2011 4:15 AM
    Moderator
  • Hi,

    At this piont i just show how to hook up command inside the datagrid then an excellent navigation take a look at the link ViewFactory.

    Add references to

    Microsoft.Expression.Interactions

    Microsoft.Practices.Prism.Interactivity

    //FindContext

    public class DataContextProxy : TriggerAction<DependencyObject>
        {

            protected override void Invoke(object parameter)
            {
                DependencyObject parent = this.AssociatedObject as DependencyObject;
                FrameworkElement fe = this.AssociatedObject as FrameworkElement;
                while (parent != null)
                {
                    if (parent.GetType() == typeof(DataGrid))
                    {
                        DataGrid context = parent as DataGrid;
                        fe.DataContext = context.DataContext;
                        break;
                    }
                    parent = VisualTreeHelper.GetParent(parent) as DependencyObject;
                }
            }
        }

    //Map xaml namespace to

    DataContextProxy

    //Hook up command inside the DataGrid

    <sdk:DataGrid Grid.Row="1" ColumnHeaderStyle="{StaticResource DataGridColumnHeaderStyle1}"
                          ItemsSource="{Binding Path=SearchResults}"
                          IsReadOnly="True" SelectedItem="{Binding Path=SearchResultSelectedItem, Mode=TwoWay}"
                          AutoGenerateColumns="False" Height="Auto" HorizontalAlignment="Left" SelectionMode="Single">
                    <sdk:DataGridTemplateColumn>
                        <sdk:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Button Margin="2" Content="Click-Me" Command="{Binding Path=ClickCommand}">
                                    <i:Interaction.Triggers>
                                <i:EventTrigger>
                                    <infra:DataContextProxy/>
                                </i:EventTrigger>
                            </i:Interaction.Triggers>                            </Button>
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellTemplate>
                    </sdk:DataGridTemplateColumn>
                </sdk:DataGrid.Columns>
            </sdk:DataGrid>

    //ViewModel

    public class MainViewModel : NotificationObject
        {
            public MainViewModel()
            {
                this.ClickCommand = new DelegateCommand(OnClickExecute);
            }
        }

    ///Command

    public DelegateCommand ClickCommand { get; private set; }

    private void OnClickExecute()
            {
                MessageBox.Show("Hello!");
            }

    http://msdn.microsoft.com/en-us/library/gg405492(v=PandP.40).aspx

     

    Thursday, October 27, 2011 4:38 AM
  • Hi,

    You're facing tree problems.

    1.How to hookup command inside the DataGrid control
    2.How to navigation in MVVM
    3.Why not store in client side rather than store it far away to server side.

    A1:

    Add references to Microsoft.Expression.Interactions and System.Windows.Interactivity

    //Implement the class like below

    namespace ForNextGeneration
    {
        public class FindContext : TriggerAction<DependencyObject>
        {

            protected override void Invoke(object parameter)
            {
                DependencyObject parent = this.AssociatedObject as DependencyObject;
                FrameworkElement fe = this.AssociatedObject as FrameworkElement;
                while (parent != null)
                {
                    if (parent.GetType() == typeof(DataGrid))
                    {
                        DataGrid context = parent as DataGrid;
                        fe.DataContext = context.DataContext;
                        break;
                    }
                    parent = VisualTreeHelper.GetParent(parent) as DependencyObject;
                }
            }
        }
    }

    //Map xaml name-space to the implementation class

    xmlns:Fx="clr-namespace:ForNextGeneration"

    //Instantiate binding to command

    <sdk:DataGrid>
                <sdk:DataGrid.Columns>
                    <sdk:DataGridTemplateColumn>
                        <sdk:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Button Margin="2" Content="Click-Me" Command="{Binding Path=ClickCommand}">
                                    <i:Interaction.Triggers>
                                        <i:EventTrigger>
                                            <Fx:FindContext/>
                                        </i:EventTrigger>
                                    </i:Interaction.Triggers>
                                </Button>
                            </DataTemplate>
                        </sdk:DataGridTemplateColumn.CellTemplate>
                    </sdk:DataGridTemplateColumn>
                </sdk:DataGrid.Columns>
            </sdk:DataGrid>

    //Implement VM;

    public class MainViewModel : NotificationObject
        {
            public MainViewModel()
            {
                 this.ClickCommand = new DelegateCommand(OnClickExecute);
            }
        }
        public DelegateCommand ClickCommand { get; private set; }
        private void OnClickExecute()
            {
                MessageBox.Show("Hello!");//Make navigation here with ViewFactory
            }

    A2:

    An Excellent navigation
    http://msdn.microsoft.com/en-us/library/gg405492(v=PandP.40).aspx

    A3

    :
    http://social.msdn.microsoft.com/Forums/en-US/silverlightmvvm/thread/b6349698-c234-4c84-b099-b610cb0af7b0//1?MVVM+Navigation+pass+query+string+parameters+

    Thursday, October 27, 2011 9:35 AM
  • Thanks for letting me know, 

    A2 link is not working, could you re post it

    Thursday, October 27, 2011 12:32 PM
  • Hi,

    A2: An excellent navigation Of Prism how to implement navigation in regular MVVM by using ViewFactory.

    http://msdn.microsoft.com/en-us/library/gg405492(v=PandP.40).aspx

     

     

    Thursday, October 27, 2011 2:35 PM