2016-11-30 48 views
0

使用混合。綁定到從XML填充的ObservableCollection不起作用

我想綁定一個GridView或TextBlock到Observable集合。可觀察集合從XML文件中獲取數據。我的測試程序運行,但當按下「顯示句子」按鈕時,GridView或TextBlock中不會顯示任何數據。

我的XML文件:

<Book ISBN ="9144252184178"> 
    <Title> 
    <TitleLine>Title First Line</TitleLine> 
    <TitleLine>Title Second Line</TitleLine> 
    </Title> 
    <Authors> 
    <Author>Some Body</Author> 
    <Author>No Body</Author> 
    </Authors> 
    <Pages> 
    <Page PageNumber ="1"> 
     <Sentences> 
     <Sentence SentenceID = "1"> 
      <SentenceText>Once there was a giant </SentenceText> 
      <SentenceFileName>9144252184178.1.1</SentenceFileName> 
      <SentenceWords> 
      <SentenceWord Start = "" Part = "">once</SentenceWord> 
      <SentenceWord Start = "" Part = "">there</SentenceWord> 
      <SentenceWord Start = "" Part = "">was</SentenceWord> 
      <SentenceWord Start = "" Part = "">a</SentenceWord> 
      <SentenceWord Start = "" Part = "">giant</SentenceWord> 
      </SentenceWords> 
     </Sentence> 
     <Sentence SentenceID = "2"> 
      <SentenceText>Every day, etc</SentenceText> 
      <SentenceFileName>9144252184178.1.2</SentenceFileName> 
      <SentenceWords> 
      <SentenceWord Start = "" Part = "">every</SentenceWord> 
      <SentenceWord Start = "" Part = "">day</SentenceWord> 
      <SentenceWord Start = "" Part = "">etc</SentenceWord> 
      </SentenceWords> 
     </Sentence> 
     </Sentences> 
    </Page> 
    </Pages> 
</Book> 

MainPage.xaml中(從一個龐大而複雜的文件,該文件在Visual Studio控制檯項目工程確定Simlpified):

(顯示一對夫婦我嘗試綁定到ObservableCollection的許多方法都失敗了)

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
    <StackPanel> 
    <Button x:Name="button" Content="Show Sentence" Click="button_Click"/> 
    <GridView Background="Bisque" ItemsSource="{x:Bind ThisSentence}"> 
     <GridView.ItemTemplate> 
     <DataTemplate x:DataType="data:Sentences"> 
      <StackPanel Background="AliceBlue"> 
      <TextBlock Text="Sentence"/> 
      <TextBlock Text="{x:Bind SentenceID}"/> 
      <TextBlock Text="{x:Bind SentenceText}"/> 
      </StackPanel> 
     </DataTemplate> 
     </GridView.ItemTemplate> 
    </GridView> 
    <Border Background="LightBlue"> 
     <TextBlock Text="{Binding ThisSentence.SentenceID, Mode=OneWay}"/> 
    </Border> 
    </StackPanel> 
</Grid> 

MainPage.xaml.cs中:

namespace ImportFromXML 
{ 
    public sealed partial class MainPage : Page 
    { 
     private ObservableCollection<Book> thisSentence_; 
     public ObservableCollection<Book> ThisSentence 
     { 
      get { return thisSentence_; } 
      set { thisSentence_ = value; } 
     } 

     public MainPage() 
     { 
      this.InitializeComponent(); 
     } 

     private void button_Click(object sender, RoutedEventArgs e) 
     { 
      var newTask = Task.Run(() => thisSentence_ = SentenceManager.GetSentence("0","0")) ; 
      DataContext = ThisSentence; 
     } 
    } 
} 

Book.cs:

(這裏是我的課,我的SentenceManager。在VS控制檯項目運行時,SentenceManager LINQ的代碼工作對我的XML。)

public class Book 
{ 
    public string ISBN { get; set; } 
    public IList<Title> title = new List<Title>(); 
    public IList<Authors> authors = new List<Authors>(); 
    public IList<Pages> pages = new List<Pages>(); 
} 

public class Title 
{ 
    public string TitleLine { get; set; } 
} 

public class Authors 
{ 
    public string AuthorName { get; set; } 
} 

public class Pages 
{ 
    public string PageNumber { get; set; } 
    public IList<Sentences> sentences = new List<Sentences>(); 
    public IList<Contents> contents = new List<Contents>(); 
} 

public class Sentences 
{ 
    public string SentenceID { get; set; } 
    public string SentenceText { get; set; } 
    public string SentenceFileName { get; set; } 
    public IList<SentenceWords> sentenceWords = new List SentenceWords>(); 
} 

public class SentenceWords 
{ 
    public string SentenceWord { get; set; } 
    public string Ending { get; set; } 
    public string Parent { get; set; } 
} 


public class SentenceManager 
{ 
    public static ObservableCollection<Book> GetSentence(string pageNumber, string sentenceID) 
    { 

     XDocument xdoc = XDocument.Load(@"C:\Users\Richard\Documents\ImportFromXML\book.xml"); 
     List<Book> sentence = (from bk in xdoc.Elements("Book") 
           select new Book 
           { 
           pages = (from pag in bk.Element("Pages").Elements("Page") 
              where (string)pag.Attribute("PageNumber") == pageNumber 
              select new Pages 
              { 
              sentences = (from sen in pag.Element("Sentences").Elements("Sentence") 
                  where (string)sen.Attribute("SentenceID") == sentenceID 
                  select new Sentences 
                  { 
                  SentenceID = sen.Attribute("SentenceID").Value, 
                  SentenceText = sen.Element("SentenceText").Value, 
                  SentenceFileName = sen.Element("SentenceFileName").Value, 
                  }).ToList(), 
              }).ToList(), 
           }).ToList(); 

     ObservableCollection <Book> Sentence = new ObservableCollection<Book>(sentence); 
     return Sentence; 
    } 
} 

在我的節目,我有綁定一些控件到我的XML數據的各個部分,所以這只是一個例子。

我是一個新手,所以請不要讓你的建議太神祕或我可能不明白!感謝你給與我的幫助。

回答

0

首先,當你綁定到ThisSentence財產x:Bind

ItemsSource="{x:Bind ThisSentence}" 

沒有必要設置DataContext可言。

然而,結合模式必須被設置爲OneWay,因爲默認是OneTime

ItemsSource="{x:Bind ThisSentence, Mode=OneWay}" 

此外,酒店通知關於值的變化。在自DependencyObject派生的類,這通常是通過使依賴屬性來完成:

public static readonly DependencyProperty ThisSentenceProperty = 
    DependencyProperty.Register(
     "ThisSentence", 
     typeof(MainPage), 
     typeof(ObservableCollection<Book>), 
     new PropertyMetadata(null)); 

public ObservableCollection<Book> ThisSentence 
{ 
    get { return (ObservableCollection<Book>)GetValue(ThisSentenceProperty); } 
    set { SetValue(ThisSentenceProperty, value); } 
} 

你也應該等待任務的Click處理程序。

private async void button_Click(object sender, RoutedEventArgs e) 
{ 
    ThisSentence = await Task.Run(() => SentenceManager.GetSentence("0", "0")); 
} 

請注意,事件處理程序是應該不存在任何async void方法的規則的例外。


如果你想使用Binding而不是x:Bind,你應該在頁面的構造一旦設定DataContext的,就像

public MainPage() 
{ 
    InitializeComponent(); 
    DataContext = this; 
} 

現在你也可以寫

ItemsSource="{Binding ThisSentence}" 
+0

我有實現了兩個建議,但在GridView或TextBlock中仍然沒有任何提示。 – RichPro

+0

根本不需要設置DataContext。見編輯的答案。 – Clemens

+0

我在typeof(MainWindow)上得到一個錯誤。無法找到類型或名稱空間名稱「MainWindow」。 – RichPro