2013-04-16 135 views
1

我有一個DataGrid的顯示,定義如下:非常奇怪的問題:涉及數據

<DataGrid Name="dgResults" 
        IsReadOnly="True" 
        AutoGenerateColumns="True" 
        AllowDrop="False" 
        CanUserAddRows="False" 
        CanUserDeleteRows="False" 
        CanUserReorderColumns="True" 
        CanUserResizeColumns="True" 
        CanUserResizeRows="False" 
        CanUserSortColumns="False" 
        ColumnWidth="120" 
        Margin="15,10,10,10" 
        Visibility="Collapsed" 
        ItemsSource="{Binding}"/> 

出於某種原因,沒有數據綁定到它時顯示。顯示正確數量的行,但它們都是空的。這是我的代碼:

dgResults.DataContext = dtTopTwoHundredResults.AsDataView(); 
dgResults.AutoGeneratingColumn += new EventHandler<DataGridAutoGeneratingColumnEventArgs>(dataGrid_AutoGeneratingColumn); 
dgResults.Visibility = Visibility.Visible; 

private void dataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) 
     { 
      //Sets the DataGrid's headers to be TextBlocks, which solves a problem whereby underscore characters in the header are ignored. 
      TextBlock block = new TextBlock(); 
      block.Text = e.Column.Header.ToString(); 
      e.Column.Header = block; 
     } 

這肯定不是與數據源的問題,因爲數據被包含在它裏面,它應該是。這只是DataGrid。這是它是如何顯示的,即正確的量行的,但沒有數據:

enter image description here

我用史努比,以找出是否該文本實際上是包含在DataGrid和我得到這個:

System.Windows.Data Error: 40 : BindingExpression path error: 'Employee identification number' property not found on 'object' ''DataRowView' (HashCode=51298929)'. BindingExpression:Path=Employee identification number. Foreign key to Employee.BusinessEntityID.; DataItem='DataRowView' (HashCode=51298929); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') 
System.Windows.Data Error: 40 : BindingExpression path error: 'Employee identification number' property not found on 'object' ''DataRowView' (HashCode=51298929)'. BindingExpression:Path=Employee identification number. Foreign key to Employee.BusinessEntityID.; DataItem='DataRowView' (HashCode=51298929); target element is 'PropertyInformation' (HashCode=11239682); target property is 'Value' (type 'Object') 
+0

你甚至可以把空格放在綁定路徑中嗎? 「員工識別號碼」 – Andy

+0

你是什麼意思@Andy?這心不是意味着是一個綁定路徑 - 這是列的名字 - 從微軟的AdventureWorks databasse –

+0

移動到這個問題,作爲我發現這個問題是什麼:http://stackoverflow.com/questions/16041222/how-to-顯示內容與標點符號 –

回答

4

在這裏的問題是,標點符號參與了列標題的事實。刪除標點符號解決了問題。希望有同樣問題的人會閱讀這篇文章,因爲我花了很多時間才意識到這一點。

0

從你的代碼看起來你只是想要自定義列標題。如果是的話,你可能會想在XAML明確定義列:

<DataGrid.Columns> 
    <DataGridTextColumn Header="Employee identification number" 
         Binding="{Binding Path=EmployeeID}" 
    </DataGridTextColumn> 
    <Insert other column definitions here/> 
</DataGrid.Columns> 

你現在的樣子,似乎XAML分析器(或其他)正在尋找屬性名相匹配什麼在那列標題(或類似的東西),而且它沒有找到與列標題匹配的ItemsSource對象的屬性。

+0

謝謝你的答案。不過,我認爲我發現了問題的根源,即列中的標點符號。進一步在這裏解釋:http://stackoverflow.com/questions/16041222/how-to-show-contents-with-punctuation-in-columns –

0

您可以顯式設置您的數據網格列,因爲這樣的問題,您的數據網格在它可以顯示的原因是有限的原因標題明確設置。問題是當你在你的autogeneratedColumn事件處理程序中設置標題時,文本塊會像你說的那樣忽略下劃線,但那點wpf不知道如何將列數據綁定到該標題。這就是爲什麼你會遇到綁定路徑錯誤。 「員工識別號碼」是它試圖綁定回你的數據視圖,但在你的數據視圖中它的「Employee_identification_number」。下面的解決方案解決了您的問題,但限制您僅顯示特定的數據視圖。如果這不是問題,那麼這是你的解決方案。

<DataGrid.Columns> 
<DataGridTextColumn Header="Employee identification number" 
        Binding="{Binding Path=EmployeeID}" 
</DataGridTextColumn> 
<Insert other column definitions here/> 
</DataGrid.Columns> 
+0

這是一個問題不幸的是,它必須能夠顯示任何數據。看看這個,我已經本地化的問題:http://stackoverflow.com/questions/16041222/how-to-show-contents-with-punctuation-in-columns –

+0

我看到的,只有這樣,我認爲這可能要做的就是把你的列轉換成可綁定的格式,也許有另一個表格包含你可以查詢的標點符號,並且在事實之後動態地創建標點符號的標題,但是這樣做絕對是一團糟。 – Bearcat9425