在WPF中绘制可调整大小的表

时间:2014-02-26 03:21:26

标签: c# .net wpf data-binding

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

现在它代表IList>,因为我应该调整它,正如我之前所说的那样。但现在我想在DataGrid中显示它,并且能够拥有行,coulmns,以及使用" Cells"。但是我无法正确绑定它,它不显示任何内容,每行只有一个空单元格。

我该怎么办?或者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的内容,但具有双向绑定...

1 个答案:

答案 0 :(得分:1)

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

如果您不想使用TreeView,我建议您使用一个简单的ListView和一个小技巧。也就是说,你可以开发一个Expander,它将添加或删除父项下面的项目,并缩进假树样,并且列中的所有单元格仍将一起调整大小。

顺便说一句,别忘了定义列

以下是:

<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事件并添加或删除所需的行。

相关问题