c#wpf mvvm DataGrid到路径的超链接

时间:2016-11-28 12:49:36

标签: c# wpf mvvm datagrid

我想创建一个DataGrid来显示我从LogFile读取的信息。 DataGrid显示LogFiles中发生的错误。读取它们并为每个错误创建对象都没有问题。

现在我需要将LogFile路径添加到DataGrid并将其设为可点击。因此,当您单击路径时,LogFile应该打开。我正在使用mvvm模式,因此不允许在代码隐藏中使用代码。

我发现了2个不同的想法,但没有一个解决它:

第一个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>

,第二个使用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>

在这两种情况下,我的Command或Handler只会在给定路径下打开文件。

我希望你理解我的问题,非常感谢我能得到的任何帮助:)

1 个答案:

答案 0 :(得分:0)

在第一种方法中,您必须执行命令绑定,如:

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

如果您的DataGrid位于UserControl而非Window,请将其更改为:

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