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:
Add the following code to create the TargetName property:
Visual Basic Copy Code 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# Copy Code public string TargetName { get; set; }
2. Once the property is created you'll propagate this to the hyperlink in the BindCellContent method:
Visual Basic Copy Code 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# Copy Code 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:
C# Copy Code public DataGridHyperlinkColumn(PropertyInfo property) : base(property)