2015-12-02 64 views
0

也許這是爲時已晚,我一直在看我的屏幕太久,但這真的讓我難住。我有datagrid顯示數據,但我加入了2個表後試圖顯示數據。數據在那裏,因爲我仍然可以使鼠標雙擊事件工作,但沒有文本顯示在任何列或行中。WPF Datagrid在行中有數據,但不顯示文本

這是XAML。

<DataGrid Grid.Column="1" Grid.Row="1" Name="ProjectsSubGrid" ItemsSource="{Binding}" 
      AutoGenerateColumns="False" IsReadOnly="True" 
      MouseDoubleClick="ProjectsSubGrid_MouseDoubleClick" 
      Initialized="ProjectsSubGrid_Initialized"> 
    <DataGrid.Columns> 
     <DataGridTextColumn Binding="{Binding Path=ProjectName}" Header="Project Name"/> 
     <DataGridTextColumn Binding="{Binding Path=AllottedHours}" Header="Allotted Hours"/> 
     <DataGridTextColumn Binding="{Binding Path=InvoicedHours}" Header="Invoiced Hours"/> 
     <DataGridTextColumn Binding="{Binding Path=UninvoicedHours}" Header="Uninvoiced Hours"/> 
     <DataGridTextColumn Binding="{Binding Path=RemainingHours}" Header="Remaining Hours"/> 
    </DataGrid.Columns> 
</DataGrid> 

這是我設置DataContext的C#代碼。

private void ProjectsSubGrid_Initialized(object sender, EventArgs e) 
{ 
    var list = from p in ProjectManagement.Context.Project 
       join a in ProjectManagement.Context.Account 
       on p.AccountId equals a.AccountId 
       select new ProjectView(){ 
        AccountId = a.AccountId, 
        ProjectId = p.ProjectId, 
        ProjectName = p.ProjectName, 
        InvoicedHours = p.InvoicedHours, 
        AccountName = a.CompanyName, 
        AllottedHours = p.AllottedHours, 
        RemainingHours = p.RemainingHours, 
        UninvoicedHours = p.UninvoicedHours 
       }; 
    ProjectsSubGrid.DataContext = list; 
} 

它適用於我使用下面的代碼,但只要我切換到它打破的連接表。

ProjectsSubGrid.DataContext = ProjectManagement.Context.Project.Where(p=>true); 

任何幫助將不勝感激。

+0

不要使用Initilized事件,更好地做到這一點在窗口的構造函數,然後檢查。 – AnjumSKhan

回答

2

感謝大家在這裏的幫助。我想這只是我昨晚看屏幕太久而已。我沒有發佈ProjectView類的代碼,這是問題所在。

我寫

public class ProjectView 
{ 
    public int ProjectId; 
    public string ProjectName; 
    public int AccountId; 
    public string AccountName; 
    public int AllottedHours; 
    public int InvoicedHours; 
    public int UninvoicedHours; 
    public int RemainingHours; 
} 

我固定它通過改變這

public class ProjectView 
{ 
    public int ProjectId { get; set; }  
    public string ProjectName { get; set; } 
    public int AccountId { get; set; } 
    public string AccountName { get; set; } 
    public int AllottedHours{ get; set; } 
    public int InvoicedHours { get; set; } 
    public int UninvoicedHours { get; set; } 
    public int RemainingHours { get; set; } 
} 
0

1)您只應在LoadGrid方法中設置的DataContext

this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => 
{ 
    ProjectsSubGrid.DataContext = list; 
})); 

2)將Name屬性設置爲DataGridTextColumn沒有意義。 3)更新您的LINQ查詢作爲

var list = (from p in ProjectManagement.Context.Project 
       join a in ProjectManagement.Context.Account 
       on p.AccountId equals a.AccountId 
       select new ProjectView(){ 
        AccountId = a.AccountId, 
        ProjectId = p.ProjectId, 
        ProjectName = p.ProjectName, 
        InvoicedHours = p.InvoicedHours, 
        AccountName = a.CompanyName, 
        AllottedHours = p.AllottedHours, 
        RemainingHours = p.RemainingHours, 
        UninvoicedHours = p.UninvoicedHours 
       }).ToList(); 
+0

我嘗試刷新和updatelayout以及初始化頁面時調用它,但沒有任何工作。我甚至刪除了DataGridTextColumn的Name屬性,並且相信它也行不通。 –

+0

從'LoadGrid'方法中有您的註釋行'ProjectsSubGrid.ItemsSource = list'。 – user1672994

+0

更新了我對第一點的回答。不要添加第2點提到的行。 – user1672994

0

嘗試增加路徑的結合DataGridTextColumn聲明。 這是我以前的一個項目的一個例子。

<DataGrid Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
              ItemsSource="{Binding Addresses}" 
              AutoGenerateColumns="False" 
              CanUserAddRows="False" 
              IsReadOnly="True" 
              SelectionMode="Single" 
              IsEnabled="{Binding IsSelectionEnabled}" 
              SelectedItem="{Binding SelectedAddress}" 
              SelectedIndex="{Binding SelectedIndex}" 
              > 
     <DataGrid.Columns> 
      <DataGridTextColumn Binding="{Binding Path=Street}" Header="Street"/> 
      <DataGridTextColumn Binding="{Binding Path=Number}" Header="Number"/> 
      <DataGridTextColumn Binding="{Binding Path=PostalCode}" Header="PostalCode"/> 
      <DataGridTextColumn Binding="{Binding Path=City}" Header="City"/> 
      <DataGridTextColumn Binding="{Binding Path=Provence}" Header="Provence"/> 
      <DataGridTextColumn Binding="{Binding Path=Country}" Header="Country"/> 
     </DataGrid.Columns> 
    </DataGrid> 
+0

謝謝,但也沒有工作。 –

+0

我想你的DataContext和Itemsource綁定一定是錯的。嘗試ItemsSource =「{綁定列表}」,然後可能嘗試刪除ProjectsSubGrid.DataContext = list;看起來你很奇怪綁定了網格的itemsource和dataContext。 (編輯:按快進入方式)。 – ImP

+0

忘記提及我的例子使用MVVM,我推測你在代碼背後做它。 [here](http://stackoverflow.com/questions/15761124/wpf-datagrid-binding-does-not-show-values)你可以找到類似於你的問題的東西。 – ImP

相關問題