【问题标题】:c# windows app listbox inside listbox binding issue(universal app phone+pc)c# windows app listbox inside listbox绑定问题(通用app phone+pc)
【发布时间】:2014-08-31 14:42:22
【问题描述】:

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

这里是 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; }
}

第二个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”在当前上下文中不存在。

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

【问题讨论】:

    标签: c# windows xaml listbox win-universal-app


    【解决方案1】:

    试试,

    拥有一个包含父列表框集合的视图模型。在该集合内有一个辅助集合来绑定子列表框的 itemssource。

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

    我为此场景准备了一个示例。 请在 http://1drv.ms/1qztFgh.

    希望对你有所帮助。

    【讨论】:

      【解决方案2】:

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

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

      2) 创建消息列表时,必须为每个 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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-28
        • 2013-05-26
        • 2011-02-08
        • 2011-03-19
        相关资源
        最近更新 更多