【问题标题】:Multi-level header GridView WPF多级标题 GridView WPF
【发布时间】:2011-06-17 04:22:41
【问题描述】:

我需要将列表视图视图设置为具有这样复杂标题的网格视图(基于我创建的 3 维对象列表):

| ---------- LEVEL 0 ------------  |
| -- Level 1a --  | -- Level 1b -- |  
| Lvl A |  Lvl B  | Lvl A  | Lvl B |

编辑:这或多或少是我的对象模型

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; }
}

我必须绑定Measures 的动态列表(这是我的第 0 级),然后绑定Threshold 的动态列表(第 1a 级...),并且每个阈值显示值 1 到 4(如果它们是)! = 0

【问题讨论】:

  • 堆叠多个gridviewrowheaderpresenter怎么样?这可能是一个好方法?我陷入了这个问题......
  • 我们能否更好地了解您的对象模型/数据是什么样的?
  • @RQDQ 我已经添加了我需要显示的对象模型

标签: c# wpf binding wpfdatagrid


【解决方案1】:

这样的事情怎么样:

<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>

您可能需要将 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;

【讨论】:

  • 谢谢,看来是个好提示,我会尽快尝试的!我唯一的疑问是我需要使用列表视图,因为到目前为止我写的对象模型重复了 n 行(代表 n 成员)。现在我正在使用更简单的解决方案(因为项目的最后期限不要等待),但我仍在寻找更优雅的解决方案!
  • @michele - 这种方法仍然适用于多个测量对象和多个阈值对象。这就是您要解决的问题吗?
  • 我试试你的代码示例。它可以工作,但我需要在列表视图的标题上设置树结构,列表视图中的行必须只显示底层值。您的示例在每一行中都设置了树,我试图避免它。我认为唯一的解决方案是编写一个完整的自定义控件。再次感谢您
  • @michele - 你没有很好地解释你需要什么。我给了你一个看起来非常像你在问题中指出的样本。我还指出它适用于多个Measures、ThresholdList 对象。也许您提供的数据对象不是您想要显示的?
  • 我最初的问题是关于获取具有多级标题的列表视图。我提供的对象模型是关于我必须绑定到标题的结构。我必须在列表视图项中显示的其他 n 个对象中包含相同的对象模型。如果我不是很清楚,我很抱歉,也许我需要更多的头脑风暴来消除我对数据结构的怀疑。再次感谢
【解决方案2】:

Level 0 是 ListView 吗?级别 1A 和级别 1b 是您想要在级别 0 列表视图内的两个不同模板列中的网格视图?那么 A 级和 b 级是 1a 级和 1b 级的另外 2 个模板列内的另外两个网格视图吗?

您也许可以将这个嵌套的 GridView 使用的相同概念转换为 WPF

http://forums.asp.net/t/1071521.aspx

我自己没有在 wpf 中尝试过,但 gridviews 似乎是类似地构建的。 只需使用面板而不是 Div,不必担心客户端/服务器端调用而减少头痛。

数据集也比列表工作得更好。

这是一些关于我之前如何设置嵌套数据集的伪代码

DataSet ds1 = new DataSet();
        ds1.Tables.Add(m_DataLayer.GetRealA_Data(X, Y).Copy()); Returns Table ["A"]
        ds1.Tables.Add(m_DataLayer.GetB_Empty().Copy());// Returns Table ["B"]
        ds1.Tables.Add(m_DataLayer.GetC_Empty().Copy());// Returns Table ["C"]
        ds1.Tables.Add(m_DataLayer.GetD_Empty().Copy());// Returns Table ["D"]

        ds1.Relations.Add("b", ds1.Tables["A"].Columns["A_id"], ds1.Tables["B"].Columns["A_id"]);
        ds1.Relations.Add("c", ds1.Tables["B"].Columns["B_id"]
            , ds1.Tables["C"].Columns["B_id"]);
        ds1.Relations.Add("d", ds1.Tables["C"].Columns["C_id"]
            , ds1.Tables["D"].Columns["D_id"]);

        dt_TheGridViewDataSet = ds1;
        TheGridView.DataSource = ds1;

【讨论】:

  • 非常感谢您的提示。不幸的是,使用嵌套的网格视图似乎不是我的问题的解决方案。我只需要一个列表视图,它显示最低级别(lvl A,lvlB):上层只是描述性的,我需要他们知道哪个对象引用 lvlA 等。
【解决方案3】:

你有没有想过它应该如何表现 - 你的标题是什么?

制作一个看起来像您建议的标题很容易 - 您可以使用一些网格以编程方式构建它 - 但这只是标题。您是否也可以像往常一样使用列表视图标题来调整它的大小?

也许您正在寻找类似 TreeListView 的东西?

http://www.codeproject.com/KB/WPF/TreeListView.aspx

我认为这就是我要显示多维数据的方式 - 它易于理解和使用,而自定义列表视图可能难以实现正确的行为。

【讨论】:

  • 感谢您的提示。当我有空闲时间时,我想试试那个控件!
猜你喜欢
  • 1970-01-01
  • 2013-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多