In the Datagrid column there are links that are formed from the 3 class properties, but nothing is done by clicking on the link.

Xaml:

<DataGridTemplateColumn Header="Имя"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock> <Hyperlink NavigateUri="{Binding Uid, StringFormat=}http://site.com/profile/{0}}" RequestNavigate="Hyperlink_RequestNavigate"> <Run Text="{Binding First_name}"/><Run Text="{Binding Last_name}"/> </Hyperlink> </TextBlock> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> 

code-behind:

 private void Hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e) { Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri)); e.Handled = true; } 

What is the problem?

UPD: I read that Datagrid has a datagridhyperlinkcolumn column for links, but as I understand it, it cannot be formed from several elements.

  • what is in Run blocks does not apply to link formation. You can write a converter to form a link from several properties - Gardes
  • @ S.Kost, Can you give an example of such a converter? For the life of me, I can't find anything. - trydex

1 answer 1

For the formation of links from several properties, it is convenient to use a multi-converter. The domain name can be transferred in the converter parameter, well, or at will.

xaml markup

 <Hyperlink RequestNavigate="Hyperlink_RequestNavigate" Name="hyperLink"> <Hyperlink.NavigateUri x:Uid="uri"> <MultiBinding Converter="{StaticResource linkConverter}" ConverterParameter="http://site.com/profile/"> <Binding Path="Name"/> <Binding Path="LastName"/> </MultiBinding> </Hyperlink.NavigateUri> <TextBlock Text="{Binding ElementName=hyperLink, Path=NavigateUri}"/> </Hyperlink> 

converter itself

 public class LinkConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { //values - ваши свойства string domainName = (string)parameter; string uriString = String.Join(string.Empty, values); //1-ый аргумент метода - разделитель ваших свойств, можно заменить на "/" string link = string.Concat(domainName, uriString); return new Uri(link); } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }