2016-04-26 40 views
2

我做了一個wpf DataGrid,它從文件名和它們的分數列表中調用。 網格工作得很好,現在我試圖讓用戶點擊一行並顯示文件內容本身(文本文件)。 文本字符串非常大,因此我不希望它是列表的屬性。相反,我希望文件路徑是屬性,每當我點擊一行時,流轉器就會讀取文件。向XAML中的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> 

謝謝你,對不起,如果我不太清楚

回答

0

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

超乎你的行爲添加到您的DataGrid中的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> 

我沒有測試過該方案,但它可能是neccessary到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); 
    } 
+0

我已經看到了這一點,不幸的是這不是我的問題的答案。 從我理解的這個角度來看,這些圖像是costumers的屬性,正如我所說 - 我不希望我的字符串是那個。 –

+0

我編輯了答案。這只是你如何解決你的問題的草圖。 :)希望它會幫助! – mikes