GridViewHyperlinkColumn binding to MVVM command

9 posts, 0 answers
  1. Chris Greening
    Chris Greening avatar
    11 posts
    Member since:
    Mar 2010

    Posted 07 May 2010 Link to this post

    Is there a way I can Bind a GridViewHyperlinkColumn so that when it is clicked a command in the ViewModel is triggered?
  2. Stefan Dobrev
    Admin
    Stefan Dobrev avatar
    790 posts

    Posted 12 May 2010 Link to this post

    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.
  3. DevCraft banner
  4. Chris Greening
    Chris Greening avatar
    11 posts
    Member since:
    Mar 2010

    Posted 12 May 2010 Link to this post

    thanks for the advice, I'll investigate.
  5. Chris Greening
    Chris Greening avatar
    11 posts
    Member since:
    Mar 2010

    Posted 12 May 2010 Link to this post

    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>

     

  6. Gaurang
    Gaurang avatar
    29 posts
    Member since:
    Dec 2010

    Posted 12 Aug 2011 Link to this post

    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
  7. Noah
    Noah avatar
    1 posts
    Member since:
    Jun 2012

    Posted 17 Jun 2012 Link to this post


    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>
  8. Noah
    Noah avatar
    1 posts
    Member since:
    Dec 2011

    Posted 17 Jun 2012 Link to this post

    resolved
  9. Mario
    Mario avatar
    6 posts
    Member since:
    Apr 2011

    Posted 22 Aug 2012 Link to this post

    Hello Noah,

    could you share how you solved it?

    Thank you very much,
        Mario
  10. ADM-IT Denis Pujdak
    ADM-IT Denis Pujdak avatar
    34 posts
    Member since:
    Sep 2011

    Posted 28 Sep 2013 Link to this post

    <!-- 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
Back to Top
DevCraft banner