You may want to add properties to a column in order to set a specific behavior. Continuing with the hyperlink column created in the previous topics, in this topic you'll add a property called TargetName. This property allows the user to specify the name of the target window or frame where the page will open.
Complete the following steps:
1. Add the following code to create the TargetName property:
Private _TargetName As String
Public Property TargetName() As String
Get
Return _TargetName
End Get
Set(ByVal value As String)
_TargetName = value
End Set
End Property
•C#
public string TargetName { get; set; }
2. Once the property is created you'll propagate this to the hyperlink in the BindCellContent method:
Public Overloads Overrides Sub BindCellContent(ByVal cellContent As FrameworkElement, ByVal row As DataGridRow)
Dim hyperlink = DirectCast(cellContent, HyperlinkButton)
If Binding IsNot Nothing Then
Dim newBinding As Binding = CopyBinding(Binding)
newBinding.Source = row.DataItem
hyperlink.SetBinding(HyperlinkButton.NavigateUriProperty, newBinding)
End If
hyperlink.HorizontalAlignment = HorizontalAlignment
hyperlink.VerticalAlignment = VerticalAlignment
hyperlink.TargetName = TargetName
End Sub
•C#
public override void BindCellContent(FrameworkElement cellContent, DataGridRow row)
{
var hyperlink = (HyperlinkButton)cellContent;
if (Binding != null)
{
Binding newBinding = CopyBinding(Binding);
newBinding.Source = row.DataItem;
hyperlink.SetBinding(HyperlinkButton.NavigateUriProperty, newBinding);
}
hyperlink.HorizontalAlignment = HorizontalAlignment;
hyperlink.VerticalAlignment = VerticalAlignment;
hyperlink.TargetName = TargetName;
}
Tips
You may find the following tips helpful when adding properties to a custom column:
• Provide a constructor that takes PropertyInfo as parameter calling base(property) in order to automatically set the Binding, SortMemberPath, FilterMemberPath and Header properties as well as properties set using custom attributes. Currently supported attributes include: DisplayAttribute (AutoGenerateFilter, Name, GroupName, Order), DisplayFormatAttribute, and EditableAttribute.
public DataGridHyperlinkColumn(PropertyInfo property) : base(property)
• You can set a converter in the binding to help you to manage scenarios where you need to use a column bound to property source that is not the same type. Suppose you want to bind a numeric column against a string property, this scenario will work correctly if you set a converter type which converts the string to a double.