2011-08-23 84 views
0

我有一個像XAML:如何在使用List動態填充wpf datagrid列文本?

<GroupBox Header="groupBox1" Height="174" HorizontalAlignment="Left" Margin="36,38,0,0" Name="groupBox1" VerticalAlignment="Top" Width="285"> 
     <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"> 
      <Grid x:Name="caseListGrid"> 
      </Grid> 
     </ScrollViewer> 
</GroupBox> 

,並在.cs文件我有一個函數:

private void LoadDataGrid() 
{ 
    caseElementList = new List<CaseElement>(); 
    caseElementList.Clear(); 

    //add 1st element 
    caseElementList.Add(new CaseElement 
    { 
     CaseName = "First Name", 
     Description = "Recently I came across the need to customize the look of the standard message box in the application. To do this I’ve decided to create a new class named WPFMessageBox" 
    }); 

    //add second 
    caseElementList.Add(new CaseElement 
    { 
     CaseName = "Second Name", 
     Description = "Recently I came across the need to customize the look of the standard message box in the application. To do this I’ve decided to create a new class named WPFMessageBox" 
    }); 

    caseElementList.Add(new CaseElement 
    { 
     CaseName = "Third Name", 
     Description = "Recently I came across the need to customize the look of the standard message box in the application. To do this I’ve decided to create a new class named WPFMessageBox" 
    }); 


    DataGrid dGrid = new DataGrid();       
    dGrid.AutoGenerateColumns = true;    
    dGrid.ItemsSource = caseElementList; 
    this.caseListGrid.Children.Add(dGrid); 
} 

在這裏我已創建自定義的情況下對象的列表功能。然後我已經將列表分配給數據網格(創建數據網格之後)。

但因爲我沒有在datagrid中添加樣式..並且文本非常大。所以, 它正在滾動查看展開。

但我想用文字環繞顯示它們。所以,任何人都可以給我任何解決方案?

我發現使用textblock可以解決。但我無法想出如何在數據網格中添加文本塊,因爲我正在動態創建這個..!

回答

0

解決方案1:不要AutoGenerateColumns並自己指定列。

解決方案2:訂閱AutoGeneratingColumn事件並將列更改爲包裝的內容(例如,使用列的ElementStyle屬性)。

+0

我已經創建了一個函數,接收事件「AutoGeneratingColumn」的事件處理程序「DataGridAutoGeneratingColumnEventArgs e」。這是我的新代碼:private void AutoGeneratingColumnEventHandler(object sender,DataGridAutoGeneratingColumnEventArgs e) {header} = e.Column.Header.ToString(); MessageBox.Show(「Header ::」+ header); } –

+0

但我只能拿着標題。但不是行的其他列表...你能給我一些建議,我怎麼寫風格... –