2011-08-20 125 views
0

我已经尝试了几个小时,就如何把多个元素具有相同名称但不同的属性,在网络上搜索的例子和有约束力的到我的XAML在我的应用程序的WP7,选择从XML多个条目具有相同名称的WP7

我解释最简单的方法是简单地告诉你,

继承人是我迄今为止

 public class Match 
    { 
     public string HomeTeam { get; set; } 
     public string AwayTeam { get; set; } 
     public string HomeScore { get; set; } 
     public string AwayScore { get; set; } 
     public string GoalsPlayer { get; set; } 
     public string goal { get; set; } 
     public string GoalsTime { get; set; } 
     public string DismissalsPlayer { get; set; } 
     public string DismissalsTime { get; set; } 
     public string BookingPlayer { get; set; } 
     public string BookingTime { get; set; } 
     public string GameTime { get; set; } 
    } 

void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
    { 
     XElement element = XElement.Parse(e.Result); 
     try 
     { 
      listBox2.ItemsSource = from item in element.Descendants("competition") 
            from match in item.Elements("match").Where(arg => arg.Attribute("awayTeam").Value == team | arg.Attribute("homeTeam").Value == team) 
            select new Match 
            { 
             HomeTeam = (string)match.Attribute("homeTeam"), 
             AwayTeam = (string)match.Attribute("awayTeam"), 
             HomeScore = (string)match.Attribute("homeTeamScore"), 
             AwayScore = (string)match.Attribute("awayTeamScore"), 
             GoalsPlayer = (string)match.Attribute("playerName"), 
             GoalsTime = (string)match.Attribute("time"), 
            }; 
     } 
     catch (Exception ex) 
     { 
      Debug.WriteLine(ex.StackTrace); 
     } 

这里是我的XAML

<ListBox Name="listBox2" Grid.Row="0"> 
        <ListBox.ItemTemplate> 
         <DataTemplate> 
          <StackPanel Name="teams" Orientation="Vertical"> 
           <StackPanel Orientation="Horizontal"> 
            <TextBlock Margin="0,10,0,0" Foreground="White" HorizontalAlignment="Left" Text="{Binding HomeTeam}"/> 
            <TextBlock Margin="10,10,30,0" Foreground="White" Text="{Binding HomeScore}" TextWrapping="Wrap" FontSize="20" /> 
            <TextBlock Margin="0,10,0,0" Foreground="White" HorizontalAlignment="Left" Text="{Binding AwayTeam}"/> 
            <TextBlock Margin="10,10,30,0" Foreground="White" Text="{Binding AwayScore}" TextWrapping="Wrap" FontSize="20" /> 
           </StackPanel> 
           <StackPanel Name="scores" Orientation="Horizontal"> 
            <TextBlock Margin="0,10,0,0" Foreground="White" Text="{Binding GoalsPlayer}" TextWrapping="Wrap" FontSize="20" /> 
            <TextBlock Margin="10,10,30,0" Foreground="White" Text="{Binding GoalsTime}" TextWrapping="Wrap" FontSize="20" /> 
           </StackPanel> 
           <StackPanel Name="dismissals"> 
            <TextBlock Foreground="White" Text="{Binding DismissalsPlayer}" TextWrapping="Wrap" FontSize="20"/> 
            <TextBlock Foreground="White" Text="{Binding DismissalsTime}" TextWrapping="Wrap" FontSize="20"/> 
           </StackPanel> 
          </StackPanel> 
         </DataTemplate> 
        </ListBox.ItemTemplate> 
       </ListBox> 

这里是我的XML

<match homeTeam="Arsenal" awayTeam="Liverpool" homeTeamScore="0" awayTeamScore="2"> 
<goal time="77:13" playerName="Aaron Ramsey" /> 
<goal time="89:57" playerName="Luis Suarez"/> 
<dismissal time="69:59" playerName="Emmanuel Frimpong"/> 
</match> 

我明白,我只会在我的页面目标的一个条目,这将是我的XML的第一个目标元素

我怎么能去有关并实现每

<goal ...> 
<goal ...> 

到我的DataTemplate,目前只显示第一个条目,另一个潜在的问题是,我不能保证有多少目标会有那么我是一个真正的上不确定如何去了解它完全

感谢

约翰

+1

你能详细说明你实际想要达到的目标?我不清楚你最近的两段。 –

+0

对不起,当然,我的数据绑定只显示在我的页面中的第一项,我相信它,因为我只处理一个项目,而且我不确定如何显示我的网页上的每个条目。 –

+0

当前页面仅显示拉姆塞的条目,不显示苏亚雷斯 –

回答

1

应该playerName甚至解决您match?因为这就是你在XML查询中解决它的地方。

无论如何,你正在寻找在这里是可以在Match对象的列表属性举行一个子对象:

listBox2.ItemsSource = from item in element.Descendants("competition") 
         from match in item.Elements("match") 
          .Where(arg => arg.Attribute("awayTeam").Value == team || 
             arg.Attribute("homeTeam").Value == team) 
         select new Match 
         { 
          HomeTeam = (string)match.Attribute("homeTeam"), 
          AwayTeam = (string)match.Attribute("awayTeam"), 
          HomeScore = (string)match.Attribute("homeTeamScore"), 
          AwayScore = (string)match.Attribute("awayTeamScore"), 
          Goals = match.Elements("goals").Select(ev => new MatchEvent 
          { 
           Player = (string)ev.Attribute("playerName"), 
           Time = (string)ev.Attribute("time") 
          }).ToList(), 
          Dismissals = match.Elements("dismissals").Select(ev => new MatchEvent 
          { 
           Player = (string)ev.Attribute("playerName"), 
           Time = (string)ev.Attribute("time") 
          }).ToList(), 
         }; 

和更新的XAML:

<ListBox Name="listBox2" Grid.Row="0"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Name="teams" Orientation="Vertical"> 
      <StackPanel Orientation="Horizontal"> 
        <TextBlock Margin="0,10,0,0" Foreground="White" HorizontalAlignment="Left" Text="{Binding HomeTeam}"/> 
        <TextBlock Margin="10,10,30,0" Foreground="White" Text="{Binding HomeScore}" TextWrapping="Wrap" FontSize="20" /> 
        <TextBlock Margin="0,10,0,0" Foreground="White" HorizontalAlignment="Left" Text="{Binding AwayTeam}"/> 
        <TextBlock Margin="10,10,30,0" Foreground="White" Text="{Binding AwayScore}" TextWrapping="Wrap" FontSize="20" /> 
       </StackPanel> 
       <ItemsControl ItemsSource="{Binding Goals}"> 
        <ItemsControl.ItemTemplate> 
         <DataTemplate> 
          <StackPanel Name="scores" Orientation="Horizontal"> 
           <TextBlock Margin="0,10,0,0" Foreground="White" Text="{Binding Player}" TextWrapping="Wrap" FontSize="20" /> 
           <TextBlock Margin="10,10,30,0" Foreground="White" Text="{Binding Time}" TextWrapping="Wrap" FontSize="20" /> 
          </StackPanel> 
         </DataTemplate> 
        </ItemsControl.ItemTemplate> 
       </ItemsControl> 

       <ItemsControl ItemsSource="{Binding Dismissals}"> 
        <ItemsControl.ItemTemplate> 
         <DataTemplate> 
          <StackPanel Name="scores" Orientation="Horizontal"> 
           <TextBlock Margin="0,10,0,0" Foreground="White" Text="{Binding Player}" TextWrapping="Wrap" FontSize="20" /> 
           <TextBlock Margin="10,10,30,0" Foreground="White" Text="{Binding Time}" TextWrapping="Wrap" FontSize="20" /> 
          </StackPanel> 
         </DataTemplate> 
        </ItemsControl.ItemTemplate> 
       </ItemsControl> 
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 
+0

能否请您解释一下这是如何工作的,也MatchEvents我认为将是一个新的类?我如何将列表绑定到DataTemplate中的单个Textblock条目? –

+0

是playerName应该,因为我需要谁拿下的人的名字, –

+1

'MatchEvent'是一个单独的类一般化的目标和解雇。你不能一个列表绑定到一个文本框,你就必须添加子'ItemsControl'到你的'ItemTemplate'并结合_its_'ItemsSource'到'MatchEvents'财产。 –

相关问题