简体   繁体   中英

Launch hyperlink event handler on datagrid cell click

I have an WPF datagrid which has some columns. One of them is a DataGridHyperlinkColumn.

Datagrid's SelectedItem event is attached to a property of my view model. This property is called MySelectedItem.

When any of the row's cells is selected property setter is fired. Setter launches a background worker to do some stuff.

Also this property is updated from view model so the changes are propagated to the view.

The problem that I have is with the DataGridHyperlinkColumn. I have attached an event handler to the hyperlink that popups a custom wpf window with a message. The problem comes here: When I click on the hyperlink and the row is not previously selected, the datagrid selecteditem event is fired, not the event handler attached to the hyperlink. The event handler attached to the hyperlink is only fired if the row has been previously selected.

The behavior I would like is:

  • If I click on the cell hyperlink the background worker should not be started, I mean, event attached to datagrid selectedItem event should not be fired. In this case only hyperlink event handler should be fired. Also datagrid row should not be marked as selected.
  • Otherwise, If I click on a datagrid cell different from the hyperlink, the datagrid selectedItem event should be fired (this last case is working without problems).

View :

<Window x:Class="DataGridExample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" 
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <my:DataGrid x:Name="MyDataGrid" 
              SelectedItem="{Binding Path=MySelectedItem}"
              ItemsSource="{Binding Path=MyListOfItems}"               
              AutoGenerateColumns="False">
        <my:DataGrid.Columns>
           <my:DataGridHyperlinkColumn Header="LinkColumn" 
               Binding="{Binding myExtraData.LinkName}" 
               ContentBinding="{Binding myExtraData.LinkValue}">
               <my:DataGridHyperlinkColumn.ElementStyle>
                 <Style TargetType="TextBlock">
                    <EventSetter Event="Hyperlink.Click" Handler="OnCellHyperlinkClick" />
                 </Style>
               </my:DataGridHyperlinkColumn.ElementStyle>
           </my:DataGridHyperlinkColumn>
        </my:DataGrid.Columns>
    </my:DataGrid>
</Grid>
</Window>

Model : MySelectedItem property is set within some points in the view model below. I do not show here to simplify.

public class MyViewModel  : ViewModelBase
{
    public MyItems MySelectedItem
    {
        get { return mySelectedItem; }

        set
        {                    
                mySelectedItem= value;

                InitiateBgWorker();

                OnPropertyChanged("MySelectedItem");
            }
        }
    }

    // Other properties, methods, etc.
}

You could handle the PreviewMouseLeftButtonDown event instead of handling the Hyperlink.Click event:

<my:DataGridHyperlinkColumn.ElementStyle>
    <Style TargetType="TextBlock">
        <EventSetter Event="PreviewMouseLeftButtonDown" Handler="OnCellHyperlinkClick" />
    </Style>
</my:DataGridHyperlinkColumn.ElementStyle>

private void OnCellHyperlinkClick(object sender, RoutedEventArgs e)
{
    e.Handled = true;

    //handle the event as before
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM