我想创建一个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只会在给定路径下打开文件。
我希望你理解我的问题,非常感谢我能得到的任何帮助:)
答案 0 :(得分:0)
在第一种方法中,您必须执行命令绑定,如:
Command="{Binding DataContext.CmdLogFilePathClick, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"
如果您的DataGrid
位于UserControl
而非Window
,请将其更改为:
Command="{Binding DataContext.CmdLogFilePathClick, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"