2

I made a wpf DataGrid which invokes from a list of file names and their score. The grid works just fine, and now I'm trying to make it available for the user to click on a row and it reveals the file content itself (text files). The text string is very large and therefore I don't want it to be an attribute of the list. Instead I want the file path to be the attribute and a streamer will read the file whenever I click a row.

I'm coding in c#. This is my (partial) code so far:

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

and my 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>

Thank you and sorry if I wasn't clear enough

Dean Cazes
  • 21
  • 3

1 Answers1

0

Here you have the answer. Is Row Details part on the blog the thing you need? At first you have to create a custom Behavior attached to your DataGrid. Let's call it for example ReadFileBehavior.

It should subscribe to RowDetailsVisibilityChanged event while attaching itself to the 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);
    }
}

Than you can add the Behavior to your DataGrid in XAML:

        <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>

I have not tested this solution, but it may be neccessary to add BindableBase inheritance to DataLeakageScorer (or implement INotifyPropertyChanged) because you may not have access to the TextBlock.Text property. Than you would just do something like this:

    private void OnRowDetailsVisibilityChanged(object sender, DataGridRowDetailsEventArgs e)
    {
        var context = e.DetailsElement.DataContext as DataLeakageScorer
        context.Score = File.ReadAllLines(context.Path);
    }
Michał J. Gąsior
  • 1,457
  • 3
  • 21
  • 39
  • I already saw this and unfortunetly this is not the answer to my question. From what I understood from this, the images are attributes of costumers and as I sayd- I don't want my string to be that. – Dean Cazes Apr 27 '16 at 00:02
  • I have edited the answer. It's only a sketch of how you can solve your problem. :) Hope it will help! – Michał J. Gąsior Apr 27 '16 at 08:10