檢查對電網的AutoGenerateColumns,高度,寬度的HorizontalAlignment和VerticalAlignment屬性的類的列表。從工具箱如果你只是拖動和拖放到你的XAML設計圖面,被生成的驗證碼
<DataGrid AutoGenerateColumns="False" Height="200" HorizontalAlignment="Left" Margin="254,64,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="200" />
將其更改爲這樣:
<DataGrid HorizontalAlignment="Stretch" Name="dataGrid1" VerticalAlignment="Stretch"/>
應該解決您的兩個問題。
編輯:你沒有指定你的列表包含什麼類型的元素,但要注意自動生成的列將被綁定到你的列表項的公共屬性。
EDIT2: 現在您添加列表項目類型,這裏有一個例子:
MainWindow.xaml.cs:
public partial class MainWindow: Window
{
public MainWindow()
{
InitializeComponent();
List<Author> list = new List<Author>
{
new Author { Name = "X Y", Content = "blah" },
new Author { Name = "W Z", Content = "blah blah" },
new Author { Name = "N N", Content = "blah blah blah" },
new Author { Name = "M M", Content = "blah blah blah blah" },
};
dataGrid1.AutoGenerateColumns = true;
dataGrid1.ItemsSource = list;
}
}
public class Author
{
public string Name { get; set; }
public DateTime PostedDate { get; set; }
public string ProjectTitle { get; set; }
public string Content { get; set; }
public string Link { get; set; }
}
MainWindow.xaml:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid HorizontalAlignment="Stretch" Name="dataGrid1" VerticalAlignment="Stretch"/>
</Grid>
</Window>
輸出:
我改變了「數據是」到「數據是」。數據是基準的複數,但將數據稱爲多項數據是不正確的。多數據仍然=在這種情況下的數據。 – 2012-02-26 09:53:47