【问题标题】:Draw resizable table in WPF在 WPF 中绘制可调整大小的表格
【发布时间】:2014-02-26 03:21:25
【问题描述】:

我的意思是我应该如何显示我的数据结构,因为我可以像使用表格一样使用它? 我希望有一个可以动态添加和删除行和列的表,但其余部分应该看起来像一个表。

现在它像 IList> 一样表示,因为我应该调整它的大小,正如我之前所说的。但现在我想在 DataGrid 中显示它,并且能够拥有行、列和使用“单元格”。但我无法正确绑定它,它不显示任何内容,每行只有一个空单元格。

我该怎么办?或者 mby 使用数组并在每行/列添加后调整它们的大小?

请指教。

现在我有了这个:

    private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
    {
        var storage = new Storage();
        storage.Data.Add(new DataRow("Bla-bla")
                     {
                         new DataEntity() {Text = "bla-bla", Forces = new[] {ForceEnum.AA}},
                         new DataEntity() {Text = "bla-bla", Forces = new[] {ForceEnum.UA}}
                     });
        DataListView.DataContext = new StorageModel(storage);
    }

public class StorageModel
{
    public StorageModel()
    {

    }

    public StorageModel(IStorage storage)
    {
        DataRowList = new ObservableCollection<DataRow>(storage.Data);
    }

    public ObservableCollection<DataRow> DataRowList
    {
        get;
        set;
    }
}
public class DataRow : IList<DataEntity>
{
    public string Name { get; private set; }
    private readonly List<DataEntity> _list = new List<DataEntity>();
...

_

    <ListView ItemsSource="{Binding DataRowList}" Name="DataListView">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ListView ItemsSource="{Binding}">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Name}" />
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

我希望能够创建类似于 this 的东西,但具有 2 向绑定...

【问题讨论】:

    标签: c# .net wpf data-binding


    【解决方案1】:

    您似乎要求的是 TreeView 而不是 ListView,因为您有一个类似树的数据结构。

    如果您不想使用 TreeView,我建议您使用带有小技巧的简单 ListView。也就是说,您可以展开一个扩展器,它会在父级下方添加或删除项目,并缩进以伪造树形外观,并且列中的所有单元格仍会一起调整大小。

    别忘了定义列

    方法如下:

    <ListView Margin="10" Name="lvUsers">
            <ListView.View>
                    <GridView>
                            <GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Name}" />
                            <GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Age}" />
                            <GridViewColumn Header="Mail" Width="150" DisplayMemberBinding="{Binding Mail}" />
                    </GridView>
            </ListView.View>
    </ListView>
    

    如果您希望实现该扩展器技巧行为,我建议您监听事件 OnExpanded 并添加或删除所需的行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-25
      • 2012-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-19
      相关资源
      最近更新 更多