【问题标题】:Adding row details to DataGrid in XAML在 XAML 中向 DataGrid 添加行详细信息
【发布时间】:2016-08-20 21:52:33
【问题描述】:

我制作了一个 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>

谢谢,如果我不够清楚,对不起

【问题讨论】:

    标签: c# xaml datagrid datatemplate rowdetails


    【解决方案1】:

    Here you have the answer. 博客上的“行详细信息”部分是您需要的吗?首先,您必须创建附加到 DataGrid 的自定义行为。让我们称之为 ReadFileBehavior。

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

    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);
        }
    

    【讨论】:

    • 我已经看到了这个,不幸的是这不是我问题的答案。据我了解,图像是客户的属性,正如我所说-我不希望我的字符串那样。
    • 我已经编辑了答案。这只是您如何解决问题的草图。 :) 希望它会有所帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    • 2014-11-17
    • 1970-01-01
    • 2010-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多