views:

109

answers:

1

Hi:

Is there any way I can associate a Command with a DataGridHyperlinkColumn? I've tried this:

   <DataGridHyperlinkColumn Header="Client Name" Binding="{Binding ShortName}">
     <DataGridHyperlinkColumn.ElementStyle>
      <Style TargetType="TextBlock">
       <Setter Property="Hyperlink.Command" 
                                    Value="{Binding DataContext.NavigateToClientCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ClientsSummaryView}}}"/>
                            <Setter Property="Hyperlink.CommandParameter" Value="{Binding}"/>
                        </Style>
                    </DataGridHyperlinkColumn.ElementStyle>
    </DataGridHyperlinkColumn>

At runtime, I can see that the binding is being correctly evaluated (the property getter for the Command is called), but the Command is not executed when I click the hyperlink. Is there a better way to do this?

Thanks,

Daniel

+2  A: 

Are you sure the command is being associated with the hyperlink? I tried setting this up in a sample app, and the command wasn't being associated with the hyperlink (if you return false from CanExecute, you'll be able to quickly determine if it is wired up).

Instead, I created a DataGridTemplateColumn to accomplish this:

<DataGridTemplateColumn Header="Client Name">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock>
                <Hyperlink Command="{Binding DataContext.NavigateToClientCommand, RelativeSource={RelativeSource AncestorType={x:Type local:ClientsSummaryView}}}"
                           CommandParameter="{Binding ShortName}">
                     <TextBlock Text="{Binding ShortName}" />
                </Hyperlink>
            </TextBlock>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

This would get really annoying if you had to create multiple columns. After cracking open the Toolkit with Reflector, it looks like it is supporting the TargetName / NavigationUrl paradigm for using the hyperlink.

If you have a scenario where you would require this type of column in many places, I would suggest extending the DataGridHyperlinkColumn and adding a Command property. You could then modify the element returned from GenerateElement so that it used your command.

Abe Heidebrecht
Abe: thanks. I was hoping to shortcut around DataGridTemplateColumn, but, honestly, it doesn't look much worse than what I had. Thanks again!
dks1983