command in datagrid not works

Topics: Prism v4 - Silverlight 4
Jan 17, 2011 at 5:56 AM
Edited Jan 18, 2011 at 8:32 AM

There is a datagrid with a template column

 

                    <data:DataGridTemplateColumn Width="180" CanUserSort="False">
                        <data:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                    <HyperlinkButton Content="View" Command="{Binding Path=DataContext.LinkButtonClickCommand,ElementName=LayoutRoot}" Style="{StaticResource GridHyperLink}"/>                                    
                            </DataTemplate>
                        </data:DataGridTemplateColumn.CellTemplate>
                    </data:DataGridTemplateColumn>

 

the DataContext is a ViewModel , and the LinkButtonClickCommand is a DelegateCommand

anyone help me why nothing happen when click the HyperlinkButton, Thanks!

Jan 17, 2011 at 6:57 PM

Hi,

The reason your HyperLink button command is not working could be that you are setting your Command Binding incorrectly. You might find the following resources useful to achieve alternative ways of binding a command from within a DataTemplate:

Also you might check your view model’s code to check if you are instantiating correctly your delegate command. You can read more about how to achieve this on Prism´s documentation, Chapter 5 Implementing MVVM Pattern, specifically the Commands section. 

Since DelegateCommands inherits from DelegateCommandBase, that implements ICommand interface, you could try replacing the DelegateCommand for a simpler implementation of ICommand, so as to make sure that the issue doesn’t come specifically from the DelegateCommand. If you find that when using that implementation you experience the same problem, you could share your concern on the Silverlight Forum, where you might find better support around Silverlight specific problems.

I hope you find this information useful.

Thanks,

Miguel Bronzovic
http://blogs.southworks.net/mbronzovic

 

Jan 18, 2011 at 8:31 AM
Edited Jan 18, 2011 at 8:32 AM

Thanks,

finally resolved by adding ViewModel as Resource. 

        [ImportingConstructor]
        public ProductList(ProductListViewModel viewModel)
        {           
            this.DataContext = viewModel;
            this.Resources.Add("ViewModel", viewModel);
            InitializeComponent();
        }

and set HyperlinkButton's Command={Binding EditCommand, Source={StaticResource ViewModel}}

 

Developer
Nov 3, 2011 at 5:16 PM

Hi,

You might find the following blog post useful, which explores one possible way to achieve this scenario using Prism's ObservableObject<T> class:

Prism: Binding to commands from inside DataTemplates

I hope you find this helpful.

Guido Leandro Maliandi 
http://blogs.southworks.net/gmaliandi

Nov 4, 2011 at 3:34 AM

Hi,

For dynamic take a look at this .http://forums.silverlight.net/t/241494.aspx/1?DataGrid+column+button+Click+navigate+to+another+page+using+MVVM+

Nov 4, 2011 at 3:35 AM

I was found there are a few way to do that.