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

GridViewHyperlinkColumn binding to MVVM command

8 Answers 707 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Chris Greening
Top achievements
Rank 1
Chris Greening asked on 07 May 2010, 03:55 PM
Is there a way I can Bind a GridViewHyperlinkColumn so that when it is clicked a command in the ViewModel is triggered?

8 Answers, 1 is accepted

Sort by
0
Stefan Dobrev
Telerik team
answered on 12 May 2010, 11:15 AM
Hello Chris,

Currently we do not expose any property on the column that can be used. My suggestion is to use CellTemplate property of the column and define your own HyperlinkButton there. This button will have its command correctly bound to your view model.

Greetings,
Stefan Dobrev
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
Chris Greening
Top achievements
Rank 1
answered on 12 May 2010, 11:30 AM
thanks for the advice, I'll investigate.
0
Chris Greening
Top achievements
Rank 1
answered on 12 May 2010, 02:33 PM
I've got this working, thanks for your advice:

<

 

 

my:GridViewDataColumn IsGroupable="True" IsFilterable="True" IsReadOnly="True" Header="Reference No." DataMemberBinding="{Binding Path=ReferenceNo}">

 

 

 

 

<my:GridViewDataColumn.CellStyle>

 

 

 

 

<Style TargetType="telerik:GridViewCell">

 

 

 

 

<Setter Property="Template">

 

 

 

 

<Setter.Value>

 

 

 

 

<ControlTemplate TargetType="telerik:GridViewCell">

 

 

 

 

<Border >

 

 

 

 

<HyperlinkButton x:Name="hlbReferenceNo" Grid.Column="0" Grid.Row="0" HorizontalContentAlignment="Center"

 

 

 

VerticalContentAlignment="Center" Tag="{Binding Path=CaseID}" Content="{Binding Path=ReferenceNo}" NavigateUri="" Click="hlbReferenceNo_Click" />

 

 

 

 

</Border>

 

 

 

 

</ControlTemplate>

 

 

 

 

</Setter.Value>

 

 

 

 

</Setter>

 

 

 

 

</Style>

 

 

 

 

</my:GridViewDataColumn.CellStyle>

 

 

 

 

</my:GridViewDataColumn>

 

0
Gaurang
Top achievements
Rank 1
answered on 12 Aug 2011, 08:11 AM
Hi All,

I have same problem as you had.

I want to implement hyperlink into my GridViewHyperlink DataTemplate and bind it to run time in my custome GridView, for that I did this.

<telerikcontrols:GridViewHyperlinkColumn x:Name="GridViewHyperLinkColumnTemplet">                               
                                    <telerikcontrols:GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <HyperlinkButton x:Name="HyperLink" Click="HyperLink_Click" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
                                        </DataTemplate>
                                    </telerikcontrols:GridViewColumn.CellTemplate>
                            </telerikcontrols:GridViewHyperlinkColumn>
 
------------------------------------------------------------------------------------------------------------------------------------
 
private void gvCalls_AutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e)
        {
            if (e.Column.UniqueName == "xyz")
            {
                GridViewHyperLinkColumnTemplet = new Telerik.Windows.Controls.GridViewHyperlinkColumn();
                //GridViewHyperLinkColumnTemplet.ContentBinding = (e.Column as Telerik.Windows.Controls.GridView).ContentBinding;
                //GridViewHyperLinkColumnTemplet.DataMemberBinding = (e.Column as Telerik.Windows.Controls.GridViewBoundColumnBase).DataMemberBinding;
                //GridViewHyperLinkColumnTemplet.DataMemberBinding = new System.Windows.Data.Binding() { Path = new PropertyPath(hrplbtn) }; //here hrplbtn is HyperlinkButton
                //GridViewHyperLinkColumnTemplet.ContentBinding = new System.Windows.Data.Binding() { Path = new PropertyPath(hrplbtn)};  //here hrplbtn is HyperlinkButton
                GridViewHyperLinkColumnTemplet.Header = e.Column.Header;
                    GridViewHyperLinkColumnTemplet.UniqueName = "xyz";                GridViewHyperLinkColumnTemplet.DisplayIndex = gvCalls.Columns.Count;
                e.Column = GridViewHyperLinkColumnTemplet;
            }
            else
            {
                e.Cancel = true;
            }
        }
-----------------------------------------------------------------------------------------------------
private void HyperLink_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(); or Open here particular child page with some calculation when user click
        }

Now when I click on hyperlink it does not work or any exception.

Anybody have any idea how to do this. When user click on hyperlink or hyperlink button, open a child page into Silverlight application.

It is appreciate to resolved my problem.

Thanks,
Gaurang
0
Noah
Top achievements
Rank 1
answered on 17 Jun 2012, 10:20 PM

Hi Stefan Dobrev
 
I did try command but it is not working. Can you provide a sample?
I am using  RadControls for Silverlight Q1 2012 SP1-  2012.1.326.1050.
Here is xaml taken from the code

<telerik:GridViewDataColumn Header="Title"
                                                                                    Width="auto"
                                                                                    DataMemberBinding="{Binding Title}">
                                                            <telerik:GridViewDataColumn.CellTemplate>
                                                                <DataTemplate>
                                                                    <HyperlinkButton Content="{Binding Title}"
                                                                                   
                                                                                     Command="{Binding GetDetails,Mode=TwoWay}"
                                                                                     CommandParameter="{Binding ID,Mode=TwoWay}" />
                                                                </DataTemplate>
                                                            </telerik:GridViewDataColumn.CellTemplate>
                                                        </telerik:GridViewDataColumn>
0
Noah
Top achievements
Rank 1
answered on 17 Jun 2012, 10:32 PM
resolved
0
Mario
Top achievements
Rank 1
answered on 22 Aug 2012, 10:14 AM
Hello Noah,

could you share how you solved it?

Thank you very much,
    Mario
0
ADM-IT Denis Pujdak
Top achievements
Rank 1
answered on 28 Sep 2013, 10:25 AM
<!-- App.xaml -->
    <Application.Resources>
        <Style x:Key="HyperlinkLikeButton" TargetType="Button">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <ContentPresenter />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HotTrackBrushKey}}" />
            <Setter Property="Cursor" Value="Hand" />
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="true">
                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="Button">
                                <ControlTemplate.Resources>
                                    <Style TargetType="{x:Type TextBlock}">
                                        <Setter Property="TextDecorations" Value="Underline" />
                                    </Style>
                                </ControlTemplate.Resources>
                                <ContentPresenter />
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Application.Resources>
 
<!-- MainWindow.xaml -->
...
                <DataGrid Name="dataGrid1" AutoGenerateColumns="False" ItemsSource="{Binding BazaBip.Companies.Items}" HorizontalGridLinesBrush="Silver" VerticalGridLinesBrush="Silver">
                    <DataGrid.Columns>
                        <DataGridTextColumn Width="40" Header="ID" Binding="{Binding id}"/>
                         <DataGridHyperlinkColumn Width="100" Header="Www" Binding="{Binding www}" >
                            <DataGridHyperlinkColumn.CellStyle>
                                <Style TargetType="DataGridCell">
                                    <Setter Property="Template">
                                        <Setter.Value>
                                            <ControlTemplate>
                                                <Button Style="{StaticResource HyperlinkLikeButton}" Content="{Binding www}" Command="{Binding www.Click}" Margin="3,0" />
                                            </ControlTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </DataGridHyperlinkColumn.CellStyle>
                        </DataGridHyperlinkColumn>
                    </DataGrid.Columns>
                </DataGrid>
...
 
<!-- Www.cs -->
    public class Www
    {
        public Www(string uri)
        {
            _uri = uri;
        }

        private string _uri;

        public override string ToString()
        {
            return _uri;
        }

        private RelayCommand _click;
        public RelayCommand Click
        {
            get
            {
                if (_click == null)
                {
                    _click = new RelayCommand(() =>
                        {
                            Process.Start(_uri);
                        });
                }
                return _click;
            }
        }
    }

<!-- CompanyViewModel.cs -->
....
        public Www www
        {
            get
            {
                if (String.IsNullOrWhiteSpace(dataEntity.www))
                {
                    return null;
                }
                else
                {
                    return new Www((!dataEntity.www.Contains("http://") ? "http://" : "") + dataEntity.www);
                }
            }
        }

....
wpf datagridhyperlinkcolumn mvvm binding command hyperlink click
Tags
GridView
Asked by
Chris Greening
Top achievements
Rank 1
Answers by
Stefan Dobrev
Telerik team
Chris Greening
Top achievements
Rank 1
Gaurang
Top achievements
Rank 1
Noah
Top achievements
Rank 1
Noah
Top achievements
Rank 1
Mario
Top achievements
Rank 1
ADM-IT Denis Pujdak
Top achievements
Rank 1
Share this question
or