Ben Clarke
Ben Clarke

Reputation: 163

How to make a cell in a DATAGRID link to another Window?

I get data from an SQL database into my DATAGRID on my WPF application. I want to be able to click a cell that is named : 'Left to audit' and from there get redirected to anothter page with all the information on how many is left to audit.

How do i go about creating the click event to take me to another page?

P.S. I am a Novice.

Edit: https://i.sstatic.net/LGnHA.png

Edit: https://i.sstatic.net/tU0bA.png - Want to click in the cells on the last column.

Upvotes: 1

Views: 1814

Answers (2)

Anders
Anders

Reputation: 1643

Try This..

Add an EventSetter on the CellStyle:

<DataGrid.CellStyle>
    <Style>
        <EventSetter Event="DataGridCell.MouseLeftButtonDown"
                     Handler="CellClicked" />
    </Style>
</DataGrid.CellStyle>

In Code Behind add Handler:

private void CellClicked(object sender, MouseButtonEventArgs e)
{
    String cellContent = ((TextBlock)sender).Text;

    xamlAllocateAudit window = new xamlAllocateAudit
    {
        DataContext = cellContent
    }
    window.Show();
}

Works on my end.. First click selects cell, second click fires handler, which opens the new window.

if you want the same window to be updated, then keep a reference of the window, if existing, update it's datacontext.

On the ohter side in the xamlAllocateAudit, create handler for the event "DataContextChanged":

<Window x:Class="WpfApplication3.xamlAllocateAudit"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="DetailsWindow" Height="300" Width="300"
    DataContextChanged="Window_DataContextChanged">
<!-- Some graphics -->
</Window>

And in CodeBehind:

private void Window_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
    var newDataContext = e.NewValue;
    //do stuff with DataContext
}

Cheers!!

Upvotes: 0

Monika
Monika

Reputation: 2210

Hope this work:

<DataGridHyperlinkColumn Binding="{Binding Link}">
    <DataGridHyperlinkColumn.ElementStyle>
        <Style>
            <EventSetter Event="Hyperlink.Click" Handler="DG_Hyperlink_Click"/>
        </Style>
    </DataGridHyperlinkColumn.ElementStyle>
</DataGridHyperlinkColumn>

private void DG_Hyperlink_Click(object sender, RoutedEventArgs e)
{
    Hyperlink link = (Hyperlink)e.OriginalSource;
    Process.Start(link.NavigateUri.AbsoluteUri);
}

If the URI points a website it will be opened with the default web-browser, if it is a folder it will be opened in explorer, if it is a file it will be opened with the default application associated with it.

To use this for autogenerated columns your property needs to be of type Uri so a DataGridHyperlinkColumn is generated. You then can hook up the event by placing the style in the DataGrid.Resources:

<DataGrid.Resources>
    <Style TargetType="Hyperlink">
        <EventSetter Event="Click" Handler="DG_Hyperlink_Click"/>
    </Style>
</DataGrid.Resources>

Upvotes: 1

Related Questions