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

Button Command not working in GridViewColumn

8 Answers 446 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Anton Swanevelder
Top achievements
Rank 2
Anton Swanevelder asked on 02 Jul 2010, 01:55 PM
Hi,

Can someone tell me how I can make this work? This is not working as it is specified below.

<telerik:GridViewColumn HeaderCellStyle="{StaticResource GridViewHeaderCellStyle1}" > 
                                    <telerik:GridViewColumn.CellTemplate> 
                                        <DataTemplate> 
                                            <Button Height="24" Style="{StaticResource GridEditButtonStyle}" Width="24" Command="{Binding DataContext.EditOrderCommand, ElementName=LayoutRoot}" CommandParameter="{Binding SelectedItem, ElementName=dgOrders}" /> 
                                        </DataTemplate> 
                                    </telerik:GridViewColumn.CellTemplate> 
                                </telerik:GridViewColumn> 

The Contents of the Command and CommandParameter is correct as it works standalone. It is just inside the GirdViewColumn that it is not.

Thanks in advance

8 Answers, 1 is accepted

Sort by
0
Yavor Georgiev
Telerik team
answered on 05 Jul 2010, 10:02 AM
Hello Anton,

 Are you using WPF or Silverlight?

Greetings,
Yavor Georgiev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Anton Swanevelder
Top achievements
Rank 2
answered on 05 Jul 2010, 12:07 PM
Hi Yavor,

Silverlight 4.0

Please note that I am binding to the DataContext of the LayoutRoot so it is not bound to the Datasource of the grid.

Thanks
0
Yavor Georgiev
Telerik team
answered on 05 Jul 2010, 03:17 PM
Hi Anton,

 Even if you try to bind it to the DataContext of a parent element, it is still tricky. You need to keep your viewmodel as a static resource and reference that. Please check out the attached sample project.

Regards,
Yavor Georgiev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Senthilraja
Top achievements
Rank 1
answered on 15 Sep 2010, 03:23 PM
Hello
Hyperlinkbutton command is not working in GridViewColumn. I checked the sample from above attachment, ViewModel is added into static resources. But we are using PRISM + MVVM, and using parameter in constructor. 

Please check the following code and give me the solution, VERY URGENT.

<telerik:RadGridView Grid.Row="1" 
                             Name="clubsGrid" 
                             ItemsSource="{Binding UserList}"
                             AutoGenerateColumns="False"                             
                             Margin="5"
                            >
                    <telerik:RadGridView.Columns>
                        <telerik:GridViewColumn Header="Actions">
                            <telerik:GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <HyperlinkButton Content="{Binding UserName}" Command="{Binding DataContext.UserNameCommand, ElementName=clubsGrid}" CommandParameter="{Binding UserID}"></HyperlinkButton>
                                </DataTemplate>
                            </telerik:GridViewColumn.CellTemplate>
                        </telerik:GridViewColumn>
                    </telerik:RadGridView.Columns>
                </telerik:RadGridView>

Thanks in advance...
0
Yavor Georgiev
Telerik team
answered on 16 Sep 2010, 09:45 AM
Hi Senthilraja,

 Silverlight does not supporting binding to a property of another element's DataContext like you have done. The usual solution is to instantiate your ViewModel as a StaticResource and reference it in your Binding's Source. However, that is not possible in Prism. To that end, you can use a DataContextProxy to bind to another element's DataContext.

All the best,
Yavor Georgiev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Juliana
Top achievements
Rank 1
answered on 16 Mar 2011, 11:15 AM
Hi there,

I also use prism and declare constructor of the view like

        public View(ViewModel viewModel)
        {
            if (App.Current.Resources.Contains("detailsViewModel"))
                App.Current.Resources.Remove("detailsViewModel");
            App.Current.Resources.Add("detailsViewModel", viewModel);

            InitializeComponent();
            this.DataContext = viewModel;
        }

In that case I can use Source={StaticResource detailsViewModel} in xaml if I need it.

HTH,
Juliana

0
Yavor Georgiev
Telerik team
answered on 17 Mar 2011, 04:01 PM
Hello Juliana,

 Yes, that is correct. However, if  you instantiate the ViewModel outside of the View (for example, when injecting the View into the RegionManager) and there is no way for you to directly reference the ViewModel type from inside the View, you can use the DataContextProxy element. It will automatically inherit the DataContext of your UserControl and it can be used as a Source of any binding in the page.

Kind regards,
Yavor Georgiev
the Telerik team
0
John
Top achievements
Rank 1
answered on 20 Jul 2012, 07:08 PM
Hello,

I'm experiencing the same issue.  I have downloaded the example but still can not get it to work.  I'm developing a WPF application. Any help is appreciated.  The company I work for has a license for the software but it is not in my name.  I can open a supprt ticket if that is needed.

Here is my XAML:
<UserControl xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"  x:Class="DCVFTool.Views.FrontPageView"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
             xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
             xmlns:views="clr-namespace:DCVFTool.Views"
             xmlns:vm="clr-namespace:DCVFTool.ViewModel"
             mc:Ignorable="d"
             Height="800" Width="950">
    <UserControl.Resources>
        <vm:FrontPageViewModel x:Key="FrontPageViewModel"/>
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot"
          Background="White"
          DataContext="{StaticResource FrontPageViewModel}">
        <telerik:RadGridView ItemsSource="{Binding GridViewTuneUps}" 
                             Width="850"
                             AutoGenerateColumns="False"
                             HorizontalAlignment="Left"
                             Margin="20,20,20,20" >
            <telerik:RadGridView.Columns>
                <telerik:GridViewColumn Header="Actions">
                    <telerik:GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Button Command="{Binding GetDetailsCommand, Source={StaticResource FrontPageViewModel}}"  CommandParameter="{Binding SelectedItem, Source={StaticResource FrontPageViewModel}}" Content="Hi"></Button>
                        </DataTemplate>
                    </telerik:GridViewColumn.CellTemplate>
                </telerik:GridViewColumn>
                <telerik:GridViewDataColumn Header="Date" DataMemberBinding="{Binding TuneUpDate}" />
                <telerik:GridViewDataColumn Header="Customer Name" DataMemberBinding="{Binding CustomerName}" />
                <telerik:GridViewDataColumn Header="Unit Id" DataMemberBinding="{Binding UnitValues, Converter={StaticResource UnitValueListToStringConverter}}" Width="150" />
                <telerik:GridViewDataColumn Header="Project Number" DataMemberBinding="{Binding ProjectNumber}" />
                <telerik:GridViewDataColumn Header="Complete" DataMemberBinding="{Binding Completed, Converter={StaticResource BooleanToYesNoConverter}}" />
                <telerik:GridViewDataColumn Header="Upload date" DataMemberBinding="{Binding UploadDate}" />
                <telerik:GridViewDataColumn Header="Type" DataMemberBinding="{Binding TuneUpTypeId,Converter={StaticResource TuneUpTypeConverter}}" />
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>       
    </Grid>
</UserControl>



Here is my View model
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;
using System.Windows;
 
namespace DCVFTool.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// </para>
    /// </summary>
    public class FrontPageViewModel : ViewModelBase
    {
        DBContext.DCVFEntityContext context;
        ObservableCollection<TuneUp> gridViewTuneUps;
 
         
        /// <summary>
        /// Initializes a new instance of the FrontPageViewModel class.
        /// </summary>
        public FrontPageViewModel()
        {
            if (IsInDesignMode)
            {
                // Code runs in Blend --> create design time data.
            }
            else
            {
                // Code runs "for real": Connect to service, etc...
                context = new DBContext.DCVFEntityContext();
 
                var tmp = from t in context.TuneUps
                          select t;
 
                GridViewTuneUps = new ObservableCollection<TuneUp>(tmp);
                 
                GetDetailsCommand = new RelayCommand<object>((parameter) =>
                {
                    MessageBox.Show(parameter.ToString());
                }
            );
                //GetDetailsCommand = new RelayCommand(() => GetDetails());
            }
 
             
             
        }
 
         
        RelayCommand<object> GetDetailsCommand { get; set; }
 
       
         
        
 
        public ObservableCollection<TuneUp> GridViewTuneUps
        {
 
            set
            {
                if (value != gridViewTuneUps)
                    gridViewTuneUps = value;
                RaisePropertyChanged("TuneUps");
 
            }
            get
            {
                return gridViewTuneUps;
            }
         
        }
 
        ////public override void Cleanup()
        ////{
        ////    // Clean own resources if needed
 
        ////    base.Cleanup();
        ////}
    }
}


Tags
GridView
Asked by
Anton Swanevelder
Top achievements
Rank 2
Answers by
Yavor Georgiev
Telerik team
Anton Swanevelder
Top achievements
Rank 2
Senthilraja
Top achievements
Rank 1
Juliana
Top achievements
Rank 1
John
Top achievements
Rank 1
Share this question
or