c#windows应用程序列表框内部列表框绑定问题(通用应用程序手机+电脑)



我正在创建一个通用应用程序,该应用程序需要在列表框中包含列表框。第一个列表框显示一个包含消息的列表。有些邮件附有照片,我必须将其添加为另一个列表。我可以用我的数据填充第一个列表框,但我无法设置第二个列表框。我通过x:Name设置ItemSource的第一个Listbox。第二个我无法通过这种方式访问。我确信我对装订有一些思考问题。

这里的xaml:

<ListBox x:Name="MessageHistory"  Background="#143b8a" Margin="0,0,0,101">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Margin="5">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <Grid Grid.Column="1">
                            <Grid.RowDefinitions>
                            </Grid.RowDefinitions>
                            <TextBlock Text="{Binding Inbox}" Margin="0,0,50,0" HorizontalAlignment="Left" TextWrapping="Wrap" FontSize="24" Foreground="WhiteSmoke" TextAlignment="Left"/>
                            <TextBlock Text="{Binding Outbox}" Margin="80,0,0,0" HorizontalAlignment="Right" TextWrapping="Wrap" FontSize="24" Foreground="Gray" TextAlignment="Right"/>
                            <!-- start of list for message photo thumbs -->
                                <ListBox ScrollViewer.HorizontalScrollBarVisibility="Auto" 
                                         x:Name="FirstThumbs" />                                   
                                    <ListBox.ItemsPanel>
                                        <ItemsPanelTemplate>
                                            <StackPanel Orientation="Horizontal" />
                                        </ItemsPanelTemplate>
                                    </ListBox.ItemsPanel>
                                    <ListBox.ItemTemplate>
                                        <DataTemplate>
                                            <Grid>
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition />
                                                    <ColumnDefinition />
                                                </Grid.ColumnDefinitions>
                                                <Image Source="{Binding         Thumb,Mode=OneWay}" Height="90" Width="90" Stretch="UniformToFill"/>
                                                <Grid Grid.Column="1">
                                                    <Grid.RowDefinitions>
                                                    </Grid.RowDefinitions>
                                                </Grid>
                                            </Grid>
                                        </DataTemplate>
                                    </ListBox.ItemTemplate>
                                </ListBox>
                                <!-- end of list for message photo thumbs   -->
                            </Grid>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

然后我有两节课。第一个是MesHistory.cs:

public class MesHistory
{
    public string Inbox { get; set; }
    public string Outbox { get; set; }
}

Second one ThumbView.cs:

public class ThumbView
{
    public string Thumb { get; set; }
}

我填充的第一个/主列表:

List<MesHistory> messageslist = new List<MesHistory>();
...foreach...{
MesHistory newMessage = new MesHistory();
newMessage.Inbox = finalmessagetext.InnerText;
messageslist.Add(newMessage);
}

然后像这样设置ItemsSource:

 MessageHistory.ItemsSource = messageslist;

当我对内部ListBox尝试同样的操作时,我无法通过名称设置ItemsSource来访问它:

List<ThumbView> newThumblist = new List<ThumbView>();
...foreach...{
ThumbView newThumb = new ThumbView();
newThumb.Thumb = thumbaddress.Attributes["src"].Value).ToString();
newThumblist.Add(newThumb);
}

然后像这样设置ItemsSource不起作用:

 FirstThumbs.ItemsSource = newThumblist;

VS告诉我"FirstThumbs"在当前上下文中不存在。

如何访问它并将数据绑定到该列表?如果可能的话,我也想为整个列表举办一个选择活动。。。

Try,

有一个具有父列表框集合的视图模型。在该集合中有一个辅助集合,用于绑定子列表框的项源。

父列表框中存在的每个项都是对应子列表框的数据上下文。

我已经为这个场景准备了一个样本。请在中找到样品http://1drv.ms/1qztFgh.

希望这对你有帮助。

1)您必须修改您的数据模型:

public class MesHistory
{
  public string Inbox { get; set; }
  public string Outbox { get; set; }
  public List<ThumbView> ThumbList {get; set;}
}

2) 创建messageslist时,必须为每个MesHistory对象填充ThumbList属性:

newMessage.ThumbList = newThumblist

3) 在XAML中修改项目模板,如下所示:

<!-- start of list for message photo thumbs -->
                            <ListBox ScrollViewer.HorizontalScrollBarVisibility="Auto"
                                     ItemSource="{Binding ThumbList}"   
                                     x:Name="FirstThumbs" />                                   
                                <ListBox.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <StackPanel Orientation="Horizontal" />
                                    </ItemsPanelTemplate>
                                </ListBox.ItemsPanel>
                                <ListBox.ItemTemplate>
                                    <DataTemplate>
                                        <Grid>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition />
                                                <ColumnDefinition />
                                            </Grid.ColumnDefinitions>
                                            <Image Source="{Binding Thumb}"
                                                   Height="90" Width="90"   Stretch="UniformToFill"/>
                                            <Grid Grid.Column="1">
                                                <Grid.RowDefinitions>
                                                </Grid.RowDefinitions>
                                            </Grid>
                                        </Grid>
                                    </DataTemplate>
                                </ListBox.ItemTemplate>
                            </ListBox>
                            <!-- end of list for message photo thumbs   -->     

4) 无法获取整个列表的选择事件。只需将其添加到主ListBox和ItemTemplate 内的ListBox

相关内容

最新更新