2016-11-30 50 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