jorame
jorame

Reputation: 2207

Using the DataGridHyperlinkcolumn to open another window and use it to pass data to the new window to pull data

I'm building a WPF application. In this application I have a DataGrid control with different columns, one of this is a DataGridHyperlinkcolumn. I would like for this hyperlink to work just like any hyperlink would in the web world. When opening the new window, I need to pass the row ID to pull the data specific to that row.

How can I accomplish this? Am I taking the wrong approach here? Sorry, I'm new to WPF.

Upvotes: 1

Views: 878

Answers (1)

codeDom
codeDom

Reputation: 1769

<Window x:Class="WpfApplication1.Window2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window2" Height="300" Width="300">
    <Grid>
        <DataGrid AutoGenerateColumns="False" Margin="10,10,12,12" Name="dataGrid1" ItemsSource="{Binding}">
            <DataGrid.Columns>
                <DataGridHyperlinkColumn Header="Header" Binding="{Binding link}" ContentBinding="{Binding content}">
                    <DataGridHyperlinkColumn.ElementStyle>
                        <Style TargetType="TextBlock">
                            <EventSetter Event="Hyperlink.Click" Handler="EventSetter_OnHandler" />
                        </Style>
                    </DataGridHyperlinkColumn.ElementStyle>
                </DataGridHyperlinkColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>
 public partial class Window2 : Window
    {

        class Data
        {
            public string link { get; set; }
            public string content { get; set; }
        }

        public Window2()
        {
            InitializeComponent();
            dataGrid1.DataContext = new object[] { new Data { link = "window2?id=3", content = "window2" } };
        }

        void EventSetter_OnHandler(object sender, RoutedEventArgs e)
        {
            var rowData = ((Hyperlink)e.OriginalSource).DataContext as Data ;
            // resolve the link ...
        }

    }

Upvotes: 0

Related Questions