【问题标题】:creating an observable collection from XML file read从读取的 XML 文件创建一个可观察的集合
【发布时间】:2014-04-14 15:33:01
【问题描述】:

我有一个这样的 xml 文件

<Root>
  <Day id="Monday">
    <subject name="Software Testing" session="2010" group="Alpha" teacher="Tasawar Khan" classroom="Class Room 1" time="8:30">

    </subject>
  </Day>
  <Day id="Tuesday">
    <subject name="Digital Image Processing" session="2011" group="Alpha" teacher="Ali Javed" classroom="Class Room 2" time="11:30">

    </subject>

  </Day>
  <Day id="Wednesday">
    <subject name="Digital Image Processing" session="2011" group="Alpha" teacher="Ali Javed" classroom="Class Room 2" time="11:30">

    </subject>
  </Day>
  <Day id="Thursday">
    <subject name="Digital Image Processing" session="2011" group="Alpha" teacher="Ali Javed" classroom="Class Room 2" time="11:30">

    </subject>
  </Day>
  <Day id="Friday">
    <subject name="Digital Image Processing" session="2011" group="Alpha" teacher="Ali Javed" classroom="Class Room 2" time="11:30">

    </subject>
  </Day>

</Root>

我正在读取此文件并将其绑定到 ListBox,如下所示

string TeachersXMLPath = "Teachers.xml";
            XDocument loadedData = XDocument.Load(TeachersXMLPath);

           var data = (from query in loadedData.Descendants("Day").Descendants("subject")

                       select new TimeTable
                       {
                           Teacher = (string)query.Attribute("teacher").Value,
                           Subject = (string)query.Attribute("name"),
                           Session = (string)query.Attribute("session"),
                           Group = (string)query.Attribute("group")
                       });

            listBox.ItemsSource = data;

这是我的时间表课程:

public class TimeTable
{
    string teacher;
    string subject;
    string session;
    string group;
    string time;
    string classroom;

    //Teacher Property

    public string Teacher
    {
        get{ return teacher;        }
        set{ teacher = value;        }
    }
    //Subject Property

    public string Subject
    {
        get { return subject; }
        set { subject = value; }
    }
    //Session Propert

    public string Session
    {
        get { return session; }
        set { session = value; }
    }
    //Group Property

    public string Group
    {
        get { return group; }
        set { group = value; }
    }
    //Time Property

    public string Time
    {
        get { return time; }
        set { time = value; }
    }

    public string Location
    {
        get { return classroom; }
        set { classroom = value; }
    }


}

这是我的列表框

<ListBox x:Name="listBox" >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Hold="teacher_detail_Hold" Margin="0,12,0,12">

                <Grid x:Name="LayoutList2" Background="BurlyWood">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>

                    <TextBlock Text="{Binding Teacher}" Style="{StaticResource PhoneTextNormalStyle}" Grid.Row="0" Grid.ColumnSpan="2" HorizontalAlignment="Center"/>
                    <TextBlock Name="subjectName" Text="{Binding Subject}" Style="{StaticResource PhoneTextNormalStyle}" Grid.Row="1" Grid.ColumnSpan="2" HorizontalAlignment="Center"/>
                    <TextBlock Text="{Binding Session}" Style="{StaticResource PhoneTextNormalStyle}" Grid.Row="2" Grid.Column="0" HorizontalAlignment="Left" />
                    <TextBlock Text="{Binding Group}" Style="{StaticResource PhoneTextNormalStyle}" Grid.Row="2" Grid.Column="1" HorizontalAlignment="Right"/>
                                <Button Grid.Row="3" Grid.Column="1" 
                                        HorizontalAlignment="Right" 
                                        Click="delete_record"
                                        BorderThickness="0">
                                    <Image Source="/Images/appbar.delete.rest.png"
                                           Width="75"
                                           Height="75"/>
                                </Button>
                            </Grid>

            </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

显示数据时效果很好。

我想要将 ObservableCollection 绑定到 ListBox,这样我还可以在单​​击删除按钮时从列表中删除所选项目。

【问题讨论】:

    标签: xml windows-phone-8 observablecollection


    【解决方案1】:

    创建一个ViewModel 类。 这个ViewModel 类应该实现INotifyPropertyChanged。 声明TimeTable 类的ObservableCollection。 将 ListBox 的 DataContext 设置为 ViewModel。 将ListBox的ItemsSource设置为ObservableCollection

    您的 ViewModel 将如下所示:

    public class SomeViewModel: INotifyPropertyChanged{
    
     private ObservableCollection<TimeTable> _timeTableCollection;
    
     public ObservableCollection<TimeTable> TimeTableCollection{
       get{
         return _timeTableCollection; 
        }
       set{     
         _timeTableCollection = value;
    onPropertyChanged(this, "TimeTableCollection"); 
       }
     }
    
    
    
    
    // Declare the PropertyChanged event
        public event PropertyChangedEventHandler PropertyChanged;
    
        // OnPropertyChanged will raise the PropertyChanged event passing the
        // source property that is being updated.
        private void onPropertyChanged(object sender, string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                PropertyChanged(sender, new PropertyChangedEventArgs(propertyName));
            }
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-29
      • 1970-01-01
      • 1970-01-01
      • 2012-03-25
      • 1970-01-01
      • 2020-04-02
      • 2016-05-14
      相关资源
      最近更新 更多