mic*_*ele 8 c# wpf binding wpfdatagrid
我需要将listview视图设置为gridview,它具有这样的复杂标题(基于我创建的3维对象列表):
| ---------- LEVEL 0 ------------ |
| -- Level 1a -- | -- Level 1b -- |
| Lvl A | Lvl B | Lvl A | Lvl B |
Run Code Online (Sandbox Code Playgroud)
编辑:这是我的对象模型
public class Measures
{
public string Caption { get; set; }
public List<Threshold> ThresholdList { get; set; }
}
public class Threshold
{
public string Caption { get; set; }
public double Value1 { get; set; }
public double Value2 { get; set; }
public double Value3 { get; set; }
public double Value4 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我要绑定一个动态列表Measures(这是我的级别0),然后是动态列表Threshold(级别1a ...),并且每个阈值显示值1到4(如果它们是!= 0)
像这样的事情怎么样:
<ListBox x:Name="MainListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label Content="{Binding Path=Caption}" HorizontalAlignment="Center" />
<ListBox Grid.Row="1" ItemsSource="{Binding Path=ThresholdList}" >
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
</StackPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label Content="{Binding Path=Caption}" HorizontalAlignment="Center" />
<StackPanel Grid.Row="1" Orientation="Horizontal">
<Label Content="{Binding Path=Value1}" />
<Label Content="{Binding Path=Value2}" />
<Label Content="{Binding Path=Value3}" />
<Label Content="{Binding Path=Value4}" />
</StackPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
您可能需要将 Value1、Value2 属性转换为集合,以便动态显示非零值,并使用我用来显示阈值的相同 ListBox/StackPanel 方法。
这给出了输出:

为了完成这篇文章,这是我使用的代码:
List<Measures> measuresList = new List<Measures>();
Measures measures = new Measures()
{
Caption = "LEVEL 0",
ThresholdList = new List<Threshold>()
};
measures.ThresholdList.Add(new Threshold()
{
Caption = "Level 1a",
Value1 = 1,
Value2 = 2,
Value3 = 3,
Value4 = 4
});
measures.ThresholdList.Add(new Threshold()
{
Caption = "Level 1b",
Value1 = 5,
Value2 = 6,
Value3 = 7,
Value4 = 8
});
measuresList.Add(measures);
this.MainListBox.ItemsSource = measuresList;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2738 次 |
| 最近记录: |