1

I've got a template, containing an other template, containing following code:

<DataGridHyperlinkColumn Header="Website" Binding="{Binding Website}" 
    IsReadOnly="True" SortMemberPath="Input.OriginalUri.AbsoluteUri" >
<DataGridHyperlinkColumn.ElementStyle>
    <Style TargetType="TextBlock">
        <EventSetter Event="Hyperlink.Click" Handler="OnHyperlinkClick" />
        <Setter Property="Tag" Value="{Binding Website}"/>
    </Style>
</DataGridHyperlinkColumn.ElementStyle>
</DataGridHyperlinkColumn>

But because it is inside a template, when I run the code, I'm getting an exception.

The usual way to deal with it is through using commands, e.g. something like

<Hyperlink CommandParameter="{Binding Website.Uri.AbsoluteUri}" Command="{Binding Navigate, ElementName=UserControl}">
<TextBlock Text= "{Binding Website.Uri.AbsoluteUri}" />
</Hyperlink>

I've tried:

<DataGridHyperlinkColumn Header="Website" Binding="{Binding Website}" 
        IsReadOnly="True" SortMemberPath="Input.OriginalUri.AbsoluteUri" >
    <DataGridHyperlinkColumn.ElementStyle>
        <Style TargetType="TextBlock">
            <Setter Property="Hyperlink.Command" Value="{Binding Navigate, ElementName=userControl}"/>
            <Setter Property="Hyperlink.CommandParameter" Value="{Binding Website}"/>               
        </Style>
    </DataGridHyperlinkColumn.ElementStyle>
</DataGridHyperlinkColumn>

but I'm getting following error:

Cannot find source for binding with reference 'ElementName=userControl'. 
BindingExpression:Path=Navigate; DataItem=null; target element is 'TextBlock' 
(Name=''); target property is 'Command' (type 'ICommand')

UserControl is declared like follows:

<UserControl x:Name="userControl" ....

How do I customize DataGridHyperlinkColumn.ElementStyle so instead of setting event handler, command would be set?

UPDATE

Command code looks like this:

    public class NavigateCommand : ICommand
    {
        #region ICommand Members

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            Process.Start(parameter.ToString());
        }

        #endregion
    }
Arsen Zahray
  • 24,367
  • 48
  • 131
  • 224
  • 2
    Have you tried `{Binding Navigate, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}` instead of ElementName binding? – LPL Jun 23 '12 at 20:54
  • I've tried that, and the error message has disappeared, and I get `Navigate` hit, but, still, the command's `CanExecute` and `Execute` never get hit – Arsen Zahray Jun 23 '12 at 21:03
  • Do you use [RelayCommand](http://msdn.microsoft.com/en-us/magazine/dd419663.aspx#id0090030) implementation? – LPL Jun 23 '12 at 21:11
  • @LPL I've implemented ICommand, but did not use additional methods implemented in NavigateCommand class – Arsen Zahray Jun 23 '12 at 21:19
  • 1
    Same question [here](http://stackoverflow.com/q/2654216/620360). – LPL Jun 23 '12 at 22:21
  • ) ended up implementing the very same thing few minutes before you posted the link – Arsen Zahray Jun 23 '12 at 22:41

1 Answers1

2

Not really the answer to your question, but maybe a workaround: to turn your Column into a template column and display a styled button within, something along the lines of:

<DataGridTemplateColumn Header="Website" 
        IsReadOnly="True" SortMemberPath="Input.OriginalUri.AbsoluteUri" >
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
           <Button Content="{Binding Website}" Command="{StaticResource NavigateCommand}" CommandParameter="{Binding Website}" Style={StaticResource LinkStyle}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
LPL
  • 16,827
  • 6
  • 51
  • 95
mindandmedia
  • 6,800
  • 1
  • 24
  • 33