wpf绑定嵌套列表 [英] wpf binding nested list

查看:97
本文介绍了wpf绑定嵌套列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在列表视图中平展此嵌套列表.

I am trying to display this nested list flat in a listview.

例如,如果EventItem有响应,则我需要在每次响应中重复事件名称x时间.并且每个响应都具有3个复选框.

For example if the EventItem has responses I need to repeat the event name x time per each reponse. And each response with have 3 checkboxes associated.

我想做的是在列表显示中:

What I am trying to do is in a list Display:

EventName->响应选项->努力复选框.

EventName -> Response Option -> Check Boxes of Effort.

我不想使用树,因此不希望使用单行.有任何想法吗?

I don't want to use a tree, hence the list single line. Any ideas?

更新:

假设我有2个事件,事件1有2个选项,事件2为3个选项.每个选项将具有3个复选框.

Lets say I have 2 events and event 1 has 2 options and event 2 as 3 options. Each option will have 3 checkboxes.

我如何尝试显示数据是这样的:

How I am trying to display the data is like this:

Event 1         Response A       [X]   [ ]   [ ]
Event 1         Response B       [ ]   [X]   [X]
Event 2         Response A       [X]   [X]   [X]
Event 2         Response D       [ ]   [ ]   [X]
Event 2         Response E       [X]   [X]   [X]

对于响应",我需要重复事件名称.

With Response I need to repeat the event name.

public class EventItem: DataAttributeChecked
{
    public EventItem(int primaryKey, string value) : base(primaryKey, value)
    {
        ResponseOptions = new List<ResponseOption>();
    }

    public List<ResponseOption> ResponseOptions { get; set; }
}

public class ResponseOption: DataAttribute
{
    public ResponseOption(int primaryKey, string value, int eventId) : base(primaryKey, value)
    {
        _eventId = eventId;
        LevelOfEfforts = new List<DataAttributeChecked>();
    }

    public List<DataAttributeChecked> LevelOfEfforts { get; set; }

    private readonly int _eventId;

    public int EventId
    {
        get { return _eventId; }
    }
}

<ListBox.ItemTemplate>
    <DataTemplate>
        <Border Margin="3" CornerRadius="2" BorderBrush="CadetBlue" BorderThickness="1">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"></ColumnDefinition>
                    <ColumnDefinition Width="*"></ColumnDefinition>
                    <ColumnDefinition Width="*"></ColumnDefinition>
                    <ColumnDefinition Width="*"></ColumnDefinition>
                </Grid.ColumnDefinitions>


                <StackPanel Grid.Column="0" Orientation="Horizontal">
                    <CheckBox IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" />

                    <TextBlock Text="{Binding Value}" VerticalAlignment="Center"/>
                </StackPanel>

                <StackPanel Grid.Column="1" Orientation="Horizontal">
                    <TextBlock Text="{Binding ResponseOption.Value}" VerticalAlignment="Center"/>   
                </StackPanel>

                <StackPanel Grid.Column="2" Orientation="Horizontal">
                    <ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
                             ItemsSource="{Binding ResponseOptions.LevelOfEffort}" 
                             Name="lstOption" 
                             SelectionMode="Multiple" >

                        <ListBox.ItemsPanel>
                            <ItemsPanelTemplate>
                                <WrapPanel IsItemsHost="True" />
                            </ItemsPanelTemplate>
                        </ListBox.ItemsPanel>

                        <ListBox.ItemContainerStyle>
                            <Style TargetType="ListBoxItem">
                                <Setter Property="IsSelected" Value="{Binding Path=IsChecked, Mode=TwoWay}"/>
                            </Style>
                        </ListBox.ItemContainerStyle>

                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal" Margin="3,3,3,3">
                                    <CheckBox IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" />
                                    <TextBlock Text="{Binding Value}" VerticalAlignment="Center"/>
                                </StackPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </StackPanel>

            </Grid>
        </Border>
    </DataTemplate>
                    </ListBox.ItemTemplate>

推荐答案

如果列表是嵌套的,则说明您有一棵树,您可以为其使用HierarchicalDataTemplate并显示在TreeView或嵌套的ListView中.

If the list is nested, you have a tree, for which you can use a HierarchicalDataTemplate and display in a TreeView or nested ListViews.

如果要在平面列表中查看,让您的ViewModel压扁树,假设您使用的是MVVM模式.

If you want to view in a flat list, have your ViewModel flatten the tree, assuming you are using an MVVM pattern.

这篇关于wpf绑定嵌套列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆