开发者

silverlight: 3 nested Collections in listboxes inside ItemsControl, bind selected items to one collection

开发者 https://www.devze.com 2023-03-28 13:03 出处:网络
I have the following object structure: An AllProducts object is an ObsCol of ProductGroups objects, a ProductGroup is an ObsCol ofLineItems, and a LineItems object is an ObsCol of LineItem objects

I have the following object structure: An AllProducts object is an ObsCol of ProductGroups objects, a ProductGroup is an ObsCol of LineItems, and a LineItems object is an ObsCol of LineItem objects

This is how I have them in my nested listboxes.

ProductGroup 1

| LineItem 1 |

| LineItem 2 |

| LineItem 3 |

ProductGroup 2

| LineItem1 |

| LineItem 2 |

| LineItem 3 |

| LineItem 4 |

Here are my ListBox:

<ItemsControl x:Name="lstProductGroups"  ItemsSource="{Binding}" ItemTemplate="{StaticResource ProductListItem}">
                开发者_开发问答        </ItemsControl>

and DataTemplates:

<DataTemplate x:Key="LineItemsTemplate">
        <StackPanel Orientation="Horizontal">
            <Image Source="{Binding ImageUrl}" Height="50" Width="50" />
            <StackPanel Orientation="Vertical" Width="350">
                <TextBlock HorizontalAlignment="Left" TextWrapping="NoWrap" Text="{Binding Name}"/>
                <TextBlock TextWrapping="Wrap" Text="{Binding Description}" HorizontalAlignment="Left"/>
            </StackPanel>
            <TextBlock TextWrapping="Wrap" Text="{Binding Price}"/>
        </StackPanel>
    </DataTemplate>
    <DataTemplate x:Key="ProductListItem">
        <StackPanel x:Name="GridDataHolder">
                <TextBlock TextWrapping="Wrap" FontWeight="Bold" Text="{Binding GroupName}" VerticalAlignment="Top" d:LayoutOverrides="Width"/>
                <ListBox x:Name="lstLineItems" ItemsSource="{Binding LineItems}" Width="500" ItemTemplate="{StaticResource LineItemsTemplate}" SelectionMode="{Binding IsListType, Converter={StaticResource BoolToSelectionModeConverter}}" ItemContainerStyle="{StaticResource LineItemsStyle}">
                </ListBox>
        </StackPanel>
    </DataTemplate>

What I'm trying to accomplish is to be able to select items in each of the listboxes and bind the selected items to an ObsCol of LineItem called "SelectedCartItems" in my VM. Any ideas?


Using the Model-View-ViewModel pattern (MVVM) wrap your ProductGroup instances in a ViewModel that expose a SelectedItem property, then bind that within your ListBox. Once you have selection in your ViewModel you can do whatever you like with it!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号