2009-12-09 20 views
3

我正在從xml文件加載列表框的值。我的問題是我無法獲得綁定顯示每個項目分配的類的屬性值。當我這樣設置文本:列表框中沒有顯示綁定值(silverlight 3)

<TextBlock Text="{Binding }" Style="{StaticResource TitleBlock}"></TextBlock> 

的項目顯示類的的toString價值,但如果我使用:

<TextBlock Text="{Binding Title}" Style="{StaticResource TitleBlock}"></TextBlock> 

我得到的名單上每個項目的空白。我希望我已經足夠好地解釋了我的問題。代碼貼在下面:

MapList.xml

<Maps> 
<map> 
    <title>Backlot</title> 
    <id>mp_backlot</id> 
    <description>Daytime urban combat.</description> 
    <thumbnail>mapImages/map11.jpg</thumbnail> 
</map> 
<map> 
    <title>Bloc</title> 
    <id>mp_bloc</id> 
    <description>Snowy close quarters combat with some sniping available.</description> 
    <thumbnail>mapImages/map11.jpg</thumbnail> 
</map> 
<map> 
    <title>The Bog</title> 
    <id>mp_bog</id> 
    <description>Night time map great for any play style.</description> 
    <thumbnail>mapImages/map11.jpg</thumbnail> 
</map> 
</Maps> 

MainPage.xaml中:

<UserControl 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" x:Class="Cod4ServerTool.MainPage" Height="521" Width="928"> 


    <Grid x:Name="LayoutRoot"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="0"/> 
      <RowDefinition/> 
     </Grid.RowDefinitions> 
    <Grid.Background> 
    <ImageBrush Stretch="Uniform" ImageSource="ui_bg.jpg"/> 
    </Grid.Background> 
    <controls:TabControl Margin="0,8,0,0" Grid.Row="1"> 
    <controls:TabControl.Background> 
    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
     <GradientStop Color="White" Offset="0"/> 
     <GradientStop Color="#662A2C12" Offset="1"/> 
    </LinearGradientBrush> 
    </controls:TabControl.Background> 
    <controls:TabItem Header="TabItem" Foreground="Black"> 
    <Grid> 
     <ListBox x:Name="MapsList_lb" Margin="8,8,177,8"> 
         <ListBox.ItemTemplate> 
          <DataTemplate> 
           <StackPanel Orientation="Horizontal"> 
            <Image Source="{Binding ThumbNail}" Style="{StaticResource ThumbNailPreview}"></Image> 
            <TextBlock Text="{Binding Title}" Style="{StaticResource TitleBlock}"></TextBlock> 
           </StackPanel> 
          </DataTemplate> 
         </ListBox.ItemTemplate> 
         <ListBox.Background> 
     <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
     <GradientStop Color="#66F0F2F0" Offset="0.254"/> 
     <GradientStop Color="#CC828C82" Offset="1"/> 
     <GradientStop Color="#CCD5DED6"/> 
     </LinearGradientBrush> 
     </ListBox.Background> 
     </ListBox> 
     <ListBox Margin="0,8,8,8" HorizontalAlignment="Right" Width="160"> 
     <ListBox.Background> 
     <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
     <GradientStop Color="#66F0F2F0" Offset="0.254"/> 
     <GradientStop Color="#CC828C82" Offset="1"/> 
     <GradientStop Color="#CCD5DED6"/> 
     </LinearGradientBrush> 
     </ListBox.Background> 
     </ListBox> 
    </Grid> 
    </controls:TabItem> 
    <controls:TabItem Header="TabItem"> 
    <Grid/> 
    </controls:TabItem> 
    </controls:TabControl> 
    <Button Height="21" HorizontalAlignment="Right" Margin="0,8,8,0" VerticalAlignment="Top" Width="95" Content="Import Maps" Grid.Row="1" Click="Button_Click"/> 
    </Grid> 
    </UserControl> 

的的.cs


    using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 

using System.Xml.Linq; 

namespace Cod4ServerTool 
{ 
    public partial class MainPage : UserControl 
    { 
     public MainPage() 
     { 
      InitializeComponent(); 
     } 

     private void Button_Click(object sender, RoutedEventArgs e) 
     { 
      DisplayMaps("MapList.xml"); 
     } 

     private void DisplayMaps(string xmlContent) 
     { 
      XDocument xmlMaps = XDocument.Load(xmlContent); 

      var maps = from map in xmlMaps.Elements("Maps").Elements("map") 
          select new Map 
          { 
           Id = map.Element("id").Value, 
           Title = map.Element("title").Value, 
           Description = map.Element("description").Value, 
           ThumbNail = map.Element("thumbnail").Value, 
          }; 

      MapsList_lb.SelectedIndex = -1; 
      MapsList_lb.ItemsSource = maps; 
     } 
    } 
} 

Map.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace Cod4ServerTool 
{ 
    class Map 
    { 
     public string Title { get; set; } 
     public string Id { get; set; } 
     public string Description { get; set; } 
     public string ThumbNail { get; set; } 

     public override string ToString() 
     { 
      return Title; 
     } 
    } 
} 
+0

看到我編輯的答案,地圖類需要公開 – AnthonyWJones 2009-12-09 13:27:34

回答

1

我會通過添加.ToList()將maps變量變成List

private void DisplayMaps(string xmlContent) 
    { 
     XDocument xmlMaps = XDocument.Load(xmlContent); 
     var maps = (from map in xmlMaps.Elements("Maps").Elements("map") 
         select new Map 
         { 
          Id = map.Element("id").Value, 
          Title = map.Element("title").Value, 
          Description = map.Element("description").Value, 
          ThumbNail = map.Element("thumbnail").Value, 
         }).ToList(); 
     MapsList_lb.SelectedIndex = -1; 
     MapsList_lb.ItemsSource = maps; 
    } 

編輯

D'哦!並且您的Map類需要聲明爲public。綁定不適用於內部類型。

使用ToList以上建議仍然有效,它更好地爲的ItemsSource引用一個簡單List<Map>比它引用LINQ查詢輪流持有至XObject秒的樹。

+0

我試過這個,我遇到了同樣的結果。 – 2009-12-09 11:52:43

+0

呵呵,我發現Map並沒有同時公開(% – bniwredyc 2009-12-09 13:52:29

+0

呵呵,謝謝!我是Silverlight和c#的全新產品(從flash分支出來),所以我知道這很簡單。額外的建議。 – 2009-12-09 18:43:38

0

實現Map類的接口INotifyPropertyChanged的:

public class Map : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 
    private string _Title; 
    public string Title 
    { 
     get { return _Title; } 
     set 
     { 
      _Title = value; 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs("Title")); 
      } 
     } 
    }  
    ... 
} 

將後正常工作。

更新:

使地圖類公開。

+0

我試過這個,我的問題仍然存在。在我的代碼中可能會出現某種錯誤。我已經通過它,這一切都很好... – 2009-12-09 11:54:05