Column with mailto: links in C1DataGrid

Posted by: gregor.sch on 5 December 2017, 1:06 am EST

    • Post Options:
    • Link

    Posted 5 December 2017, 1:06 am EST

    Hi everyone,

    I’m currently working on a grid which contains contact information, including e-mail addresses. I would like to format the (databound) content of this column as a mailto: link, but i’m stuck.

    Can anyone provide any info on how to implement such a behaviour? Can this be achieved by only modifying the xaml-templates for hyperlinkcolumns or hyperlinkbuttons?

    Thanks in advance!

  • Posted 5 December 2017, 5:10 pm EST

    Hi,

    I suggest you to use DataGridTemplateColumn for making a custom template column as below:

    <c1:DataGridTemplateColumn Header="Mail To" HorizontalAlignment="Center">
                        <c1:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>                            
                                    <TextBlock Name="textblock" HorizontalAlignment="Center"  VerticalAlignment="Center" Foreground="Blue">
                                    <Underline>
                                      <Run Text="{Binding Mailto}" MouseLeftButtonDown="Run_MouseLeftButtonDown">                                     
                                      </Run>
                                    </Underline>
                                        <TextBlock.Style>
                                        <Style>
                                            <Style.Triggers>
                                                <Trigger Property="Border.IsMouseOver" Value="True">
                                                    <Setter Property="Border.Cursor" Value="Hand"/>
                                                </Trigger>
                                            </Style.Triggers>
                                        </Style>
                                    </TextBlock.Style>
                                    </TextBlock>
                            </DataTemplate>
                        </c1:DataGridTemplateColumn.CellTemplate>
                    </c1:DataGridTemplateColumn>
    

    Write your mail sending code in an event:

    private void Run_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                // Write your code here to send mail
            }
    

    Hope, it will help you.

    Thanks,

    Singh

  • Posted 6 December 2017, 12:15 am EST

    Hi Singh,

    Using a Templatecolumn was one of my first ideas too, formatting a textblock to resemble a hyperlink is the easy part in WPF. However, using a TemplateColumn is not an option for me since It’s incompatible with column filtering and sorting.

    For some reason, the hyperlink control inside the C1HyperlinkButton doesn’t recognize the “mailto:” schema and would always open “http://mailto:name@domain.ext/” in the browser. After a lot of trial and error I ended up modifying the hyperlinkcolumn controltemplate (so it would forward the address as-is, without the http schema) and handling the rest from code-behind.

    Thanks for your effort!

    G

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels