我有一个这样的对象:
public class MChiStructure
{
[System.Runtime.Serialization.OptionalField(VersionAdded = 2)]
public double chiV1Plus;
[System.Runtime.Serialization.OptionalField(VersionAdded = 2)]
public double chiV1Minus;
[System.Runtime.Serialization.OptionalField(VersionAdded = 2)]
public double mV1Plus;
...
}
我希望有一个DataGrid,在第一列中显示变量名称,在第二列中显示double值。 我也想知道是否有可能隐藏一些值(例如,如果一个是否定的,则必须隐藏它)
全部谢谢
答案 0 :(得分:1)
它是应用程序背后的代码,起初应该创建一个模型类并填充代码隐藏。就这样。请看一个例子:
<强>型号:强>
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; }
}
看起来像这样: