Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cell Template with Hyperlink usage in MVVM pattern

I'm tring to insert an hyperlink within a DataGrid and find a method in order to implement the RequestNavigate behaviour using the MVVM pattern.

I've tried a lot of solutions up until now, but none of them works. Could you help me, please?

This is my xaml code:

<dgWPFCtrl:ExtDataGridTemplateColumn  Header="Link to XXX"  Width="*">
                    <dgWPFCtrl:ExtDataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock >
                                <Hyperlink NavigateUri="{Binding Path=ID_HTTP_LINK}"
                                           >
                                    <TextBlock Text="{Binding Path=ID_HTTP_LINK}"/>
                                    <i:Interaction.Triggers>
                                        <i:EventTrigger EventName="RequestNavigate">
                                            <WPFCtrl:EventToCommand 
                                                PassEventArgsToCommand="True"
                                                Command="{Binding Path=OpenLinkCommand}" />
                                        </i:EventTrigger>
                                    </i:Interaction.Triggers>
                                </Hyperlink>
                            </TextBlock>
                        </DataTemplate>
                    </dgWPFCtrl:ExtDataGridTemplateColumn.CellTemplate>
                </dgWPFCtrl:ExtDataGridTemplateColumn>

and following the relative ICommand development:

//Command for open link
RelayCommand _openLinkCommand;
public ICommand OpenLinkCommand
{
    get
    {
        if (_openLinkCommand == null)
            _openLinkCommand = new RelayCommand(param => 
            {
                //Command Body ...
            });
        return _openLinkCommand;
    }
}

Where I'm wrong? Unexpectedly, the ICommand is never even called!

I've tried to use also other kind of event ( such as MouseEnter), but nothing changed!

Thanks in advance for your contributions,

Deby

like image 489
Deby Avatar asked Mar 04 '13 14:03

Deby


1 Answers

The Hyperlink's DataContext is that of the object represented by the DataGridRow, not your ViewModel. So you'll have to use a binding method to get you to that ViewModel (either RelativeSource AncestorType, or ElementName).

ElementName (assuming your DataGrid is named 'myDataGrid')

Command="{Binding ElementName=myDataGrid, Path=DataContext.OpenLinkCommand}"

RelativeSource

Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.OpenLinkCommand}"
like image 110
ZF. Avatar answered Nov 13 '22 04:11

ZF.