c# - 在 XAML 中向 DataGrid 添加行详细信息

标签 c# xaml datagrid datatemplate rowdetails

我制作了一个 wpf DataGrid,它从文件名列表及其分数中调用。 网格工作得很好,现在我试图让用户可以点击一行,它会显示文件内容本身(文本文件)。 文本字符串非常大,因此我不希望它成为列表的属性。相反,我希望文件路径成为属性,每当我单击一行时,流媒体就会读取该文件。

我正在用 C# 编写代码。到目前为止,这是我的(部分)代码:

public class DataLeakageScorer
{
    public string fileName { get; set; }
    public string score { get; set; }
    public string path { get; set; }

    public DataLeakageScorer(string fileName, string score, string path)
    {
        this.fileName = fileName;
        this.score = score;
        this.path = path;
    }
}

和我的 XAML:

<Grid>
    <Button x:Name="button" Content="Browse" HorizontalAlignment="Left" Margin="464,22,0,0" VerticalAlignment="Top" Width="75" Click="browse"/>
    <Label x:Name="status" Content="Please select a folder" FontSize="15" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Center" Margin="0,5,121,0" VerticalAlignment="Top" Width="459" Height="52"/>
    <DataGrid Name="scoresTable" AutoGenerateColumns="False" IsReadOnly="True" CanUserSortColumns="False" Margin="0,62,0,0">
        <DataGrid.Columns>
            <DataGridTextColumn Header="File Name" Binding="{Binding fileName}"/>
            <DataGridTextColumn Header="Score" Binding="{Binding score}"/>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

谢谢,抱歉,如果我不够清楚

最佳答案

Here you have the answer.博客上的 Row Details 部分是您需要的吗?首先,您必须创建附加到 DataGrid 的自定义行为。我们称它为 ReadFileBehavior。

它应该在将自己附加到 DataGrid 的同时订阅 RowDetailsVisibilityChanged 事件。

public class ReadFileBehavior : Behavior<DataGrid>
{
    protected override void OnAttached()
    {
        base.OnAttached();
        this.AssociatedObject.RowDetailsVisibilityChanged += OnRowDetailsVisibilityChanged;
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        this.AssociatedObject.RowDetailsVisibilityChanged -= OnRowDetailsVisibilityChanged;
    }

    private void OnRowDetailsVisibilityChanged(object sender, DataGridRowDetailsEventArgs e)
    {
        var element = e.DetailsElement as TextBlock;
        element.Text = File.ReadAllLines((element.DataContext as DataLeakageScorer).Path);
    }
}

然后您可以在 XAML 中将行为添加到您的 DataGrid:

        <DataGrid ItemsSource="{Binding Customers}">
            <Interactivity:Interaction.Behaviors>
                <customBehaviors:ReadFileBehavior />
            </Interactivity:Interaction.Behaviors>
            <DataGrid.Columns>
                <DataGridTextColumn Header="First Name" Binding="{Binding FirstName}" />
            </DataGrid.Columns>
            <DataGrid.RowDetailsTemplate>
                <DataTemplate>
                    <TextBlock />
                </DataTemplate>
            </DataGrid.RowDetailsTemplate>
        </DataGrid>

我尚未测试此解决方案,但可能需要将 BindableBase 继承添加到 DataLeakageScorer(或实现 INotifyPropertyChanged),因为您可能无权访问 TextBlock.Text 属性。比你会做这样的事情:

    private void OnRowDetailsVisibilityChanged(object sender, DataGridRowDetailsEventArgs e)
    {
        var context = e.DetailsElement.DataContext as DataLeakageScorer
        context.Score = File.ReadAllLines(context.Path);
    }

关于c# - 在 XAML 中向 DataGrid 添加行详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36875597/

相关文章:

c# - 将按钮插入 NavigationViewItem UWP

c# - WPF:自定义消息框

.net - WPF 中堆栈面板鼠标悬停上的动画文本 block

C# WPF DataGrid 在列中搜索一个值,返回行索引

c# - 如何禁用 Datagrid 中的特定列排序?

具有不同 ItemsSource 的 WPF MVVM DataGrid RowDetails

c# - ASP.NET 核心,更改未经授权的默认重定向

c# - C# 中多个数据集 Winform 项目的 RDLC 问题

c# - 如何修改 web 服务代理以获取原始 XML

c# - 找到包含特定子元素的父元素