简体   繁体   中英

c# wpf mvvm DataGrid Hyperlink to Path

I want to create a DataGrid to show informations I read from a LogFile . The DataGrid displays the occured errors, found in LogFiles . Reading them and creating Objects for each error is no problem.

Now I need to add the LogFile path to the DataGrid and make it clickable. So when you click on the path the LogFile should open. Im using mvvm pattern, so no code in code-behind allowed.

I found 2 different ideas to my problem but none of them solved it:

First one with an TemplateColumn :

<DataGrid HorizontalAlignment="Stretch"
         VerticalAlignment="Stretch"
         Margin="10"
         ItemsSource="{Binding Path=Errors}"
         SelectionMode="Single"
         SelectedItem="{Binding Mode=OneWayToSource, Path=SelectedError}"
         AutoGenerateColumns="False"
         IsReadOnly="True">
    <DataGrid.Columns>
        <DataGridTextColumn Header="{Binding Path=Data.Texts[FaultyFileCaption], Source={StaticResource DataContextProxy}}" Binding="{Binding Path=FaultyFile}" Width="Auto"/>
        <DataGridTextColumn Header="{Binding Path=Data.Texts[LanguageCaption], Source={StaticResource DataContextProxy}}" Binding="{Binding Path=Language}" Width="Auto"/>
        <DataGridTextColumn Header="{Binding Path=Data.Texts[KindCaption], Source={StaticResource DataContextProxy}}" Binding="{Binding Path=Type}" Width="Auto"/>
        <DataGridTextColumn Header="{Binding Path=Data.Texts[ErrorCodeCaption], Source={StaticResource DataContextProxy}}" Binding="{Binding Path=ErrorCode}" Width="Auto"/>
        <DataGridTextColumn Header="{Binding Path=Data.Texts[SourcePathCaption], Source={StaticResource DataContextProxy}}" Binding="{Binding Path=SourcePath}" Width="Auto"/>
        <DataGridTemplateColumn Header="{Binding Path=Data.Texts[LogFilePath], Source={StaticResource DataContextProxy}}" Width="Auto">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock>
                        <Hyperlink Command="{Binding ElementName=ErrorDataGrid, Path=Data.CmdLogFilePathClick, Source={StaticResource DataContextProxy}}"/>
                        <TextBlock Text="{Binding Path=LogFilePath}"/>
                    </TextBlock>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

and the second one using an HyperlinkColumn :

<DataGrid HorizontalAlignment="Stretch"
         VerticalAlignment="Stretch"
         Margin="10"
         ItemsSource="{Binding Path=Errors}"
         SelectionMode="Single"
         SelectedItem="{Binding Mode=OneWayToSource, Path=SelectedError}"
         AutoGenerateColumns="False"
         IsReadOnly="True"
         Name="ErrorDataGrid">

    <DataGrid.Columns>
        <DataGridTextColumn Header="{Binding Path=Data.Texts[FaultyFileCaption], Source={StaticResource DataContextProxy}}" Binding="{Binding Path=FaultyFile}" Width="Auto"/>
        <DataGridTextColumn Header="{Binding Path=Data.Texts[LanguageCaption], Source={StaticResource DataContextProxy}}" Binding="{Binding Path=Language}" Width="Auto"/>
        <DataGridTextColumn Header="{Binding Path=Data.Texts[KindCaption], Source={StaticResource DataContextProxy}}" Binding="{Binding Path=Type}" Width="Auto"/>
        <DataGridTextColumn Header="{Binding Path=Data.Texts[ErrorCodeCaption], Source={StaticResource DataContextProxy}}" Binding="{Binding Path=ErrorCode}" Width="Auto"/>
        <DataGridTextColumn Header="{Binding Path=Data.Texts[SourcePathCaption], Source={StaticResource DataContextProxy}}" Binding="{Binding Path=SourcePath}" Width="Auto"/>
        <DataGridHyperlink Header="{Binding Path=Data.Texts[LogFilePath], Source={StaticResource DataContextProxy}}" Binding="{Binding Path=LogFilePath}" Width="Auto">
            <DataGridHyperlinkColumn.ElementStyle>
                <Style>
                    <EventSetter Event="Hyperlink.Click" Handler="{Binding Path=Data.DG_Hyperlink_Click, Source={StaticResource DataContextProxy}}"/>
                </Style>
            </DataGridHyperlinkColumn.ElementStyle>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

In both cases my Command or Handler does only open the file at the given path.

I hope you understand my problem and many thanks for any help I can get :)

In your first approach you have to do the Command-Binding like:

Command="{Binding DataContext.CmdLogFilePathClick, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"

If your DataGrid is located in a UserControl and not a Window change it to:

Command="{Binding DataContext.CmdLogFilePathClick, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"

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