wpf 绑定嵌套列表

发布于 2024-10-30 02:21:59 字数 4252 浏览 2 评论 0原文

我正在尝试在列表视图中显示此嵌套列表。

例如,如果 EventItem 有响应,我需要为每个响应重复事件名称 x 次。每个响应都有 3 个关联的复选框。

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

EventName ->响应选项->勾选努力框。

我不想使用树,因此列表单行。有什么想法吗?

更新:

假设我有 2 个事件,事件 1 有 2 个选项,事件 2 有 3 个选项。每个选项都有 3 个复选框。

我尝试显示数据的方式如下:

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]

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

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>

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

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 -> Response Option -> Check Boxes of Effort.

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

UPDATE:

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没企图 2024-11-06 02:21:59

如果列表是嵌套的,你就有一棵树,
您可以使用 HierarchicalDataTemplate
并显示在 TreeView 或嵌套 ListView 中。

如果您想以平面列表形式查看,
让你的 ViewModel 压平树,
假设您使用的是 MVVM 模式。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文