2011-04-17 72 views
0

我正在开发Windows Phone应用程序。DataBinding不工作

我有下面的C#代码:

private void Default_Loaded(object sender, RoutedEventArgs e) 
{ 
    long gameId; 
    short gameType; 
    loadedData = XDocument.Load("SampleData/GamesDesc.xml"); 
    if (NavigationContext.QueryString.Count == 2) 
    { 
     if (long.TryParse(NavigationContext.QueryString.Values.First(), out gameId)) 
     { 
      if (short.TryParse(NavigationContext.QueryString.Values.Last(), out gameType)) 
      { 
       var filteredData = from c in loadedData.Descendants("gameDescription") 
        where c.Attribute("game_id").Value == gameId.ToString() && 
          c.Attribute("gameType").Value == gameType.ToString() && 
          c.Attribute("language").Value.Equals(CultureInfo.CurrentCulture.TwoLetterISOLanguageName) 
        select new SampleData.GameDesc() 
        { 
         Id = uint.Parse(c.Attribute("game_id").Value), 
         Name = c.Attribute("name").Value, 
         Language = c.Attribute("language").Value, 
         GameType = uint.Parse(c.Attribute("gameType").Value), 
         ShortDescription = c.Attribute("shortDescription").Value, 
         LongDescription = c.Attribute("longDescription").Value 
        }; 

       LayoutRoot.DataContext = filteredData; 
      } 
     } 
    } 
} 

而下面的XAML代码:

<!--LayoutRoot is the root grid where all page content is placed--> 
<Grid x:Name="LayoutRoot" Background="Transparent"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 

    <!--TitlePanel contains the name of the application and page title--> 
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> 
     <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/> 
     <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 
    </StackPanel> 

    <!--ContentPanel - place additional content here--> 
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
     <Grid x:Name="LongDescPanel" Margin="0"> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="0.45*"/> 
       <RowDefinition Height="0.55*"/> 
      </Grid.RowDefinitions> 
      <TextBlock x:Name="LongDescription" Margin="8,8,8,13" TextWrapping="Wrap" Text="{Binding LongDescription}"/> 
      <Button x:Name="PlayButton" Content="{Binding Path=AppResources.Play, Source={StaticResource LocalizedStrings}}" VerticalAlignment="Top" Margin="165,36,165,0" d:LayoutOverrides="Width" Grid.Row="1" Click="PlayButton_Click" /> 
     </Grid> 
    </Grid> 
</Grid> 

为什么LongDescription TextBlock中确实表现出什么?

回答

1

要设置一个IEnumberable<SampleData.GameDesc>LayoutRoot的DataContext。这就是为什么它无法读取名为LongDescription的属性。

尝试设置单一元素作为的DataContext

LayoutRoot.DataContext = filteredData.First(); 

如果你想显示所有项目,而不只是第一个,使用一个ItemsControl的

+0

谢谢。现在正在工作。 – VansFannel 2011-04-17 07:22:54