它,它的背後是應用程序代碼中,在第一你應該創建一個模型類並填充它的代碼隱藏。就這樣。請,看一個例子:
型號:窗口的
public class MChiStructure
{
public string TitleField { get; set; }
public double chiV1Plus { get; set; }
public double chiV1Minus { get; set; }
public double mV1Plus { get; set; }
}
代碼隱藏:
public MainWindow()
{
InitializeComponent();
FillDataGrid();
}
private void FillDataGrid()
{
ObservableCollection<MChiStructure> coll = new ObservableCollection<MChiStructure>();
for (int start = 0; start < 10; start++)
{
coll.Add(new MChiStructure(){TitleField="Title " + start.ToString(),
chiV1Minus=start-1, chiV1Plus=start+1, mV1Plus=start-1});
}
dataGrid.ItemsSource = coll;
}
和XAML:
<DataGrid Name="dataGrid"/>
更新。使用ListView控件:
XAML:
<ListView Name="listView">
<ListView.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Brown" BorderThickness="1" CornerRadius="5">
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="chiV1Plus:" Margin="2" FontWeight="Bold"/>
<TextBlock Text="{Binding chiV1Plus}" Margin="2" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="chiV1Minus:" Margin="2" FontWeight="Bold"/>
<TextBlock Text="{Binding chiV1Minus}" Margin="2" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="mV1Plus:" Margin="2" FontWeight="Bold" />
<TextBlock Text="{Binding mV1Plus}" Margin="2" />
</StackPanel>
</StackPanel>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
C#代碼隱藏:
public MainWindow()
{
InitializeComponent();
FillListView();
}
private void FillListView()
{
List<MChiStructure> coll = new List<MChiStructure>();
for (int start = 0; start < 10; start++)
{
coll.Add(new MChiStructure()
{
TitleField = "Your Title: " + start.ToString(),
chiV1Minus = start - 1,
chiV1Plus = start + 1,
mV1Plus = start - 1
});
}
listView.ItemsSource = coll;
}
型號:
public class MChiStructure
{
public string TitleField { get; set; }
public double chiV1Plus { get; set; }
public double chiV1Minus { get; set; }
public double mV1Plus { get; set; }
}
它看起來像這樣:
你確定你想要DataGrid嗎?在我看來,最好是使用ListView?爲什麼選擇DataGrid? – StepUp
你如何使用ListView?用您的解決方案發布答案,謝謝! – FrancescoDS