jeudi 4 août 2011

Binding data from xml file to WPF application

pour afficher contenu d un xml file dans une list dans un wpf

fichier xml :
<?xml version="1.0" encoding="utf-8" ?>
<People>
  <Person Name="Capt. Monterey Jack">
    <ImageFile>Data/MontereyJack.jpg</ImageFile>
    <Notes>The Captain loves his cheese, but hates milk.</Notes>
  </Person>

au debut on doit declarer la source xml



<Grid.DataContext>
                        <XmlDataProvider
        Source="Data/People.xml"
        XPath="/People/Person"
        />
                    </Grid.DataContext>

dans la gride de la wpf ensuite on cree la listbox
           <ListBox
    
      IsSynchronizedWithCurrentItem="True"
      ItemsSource="{Binding XPath=ImageFile}">//image file est un chmop xml q on veu
                            <ListBox.ItemContainerStyle>
                                <!-- Force the items to fill all available space. -->
                                <Style TargetType="ListBoxItem">
                                    <Setter
            Property="HorizontalContentAlignment"
            Value="Stretch"
            />
                                </Style>
                            </ListBox.ItemContainerStyle>
                        </ListBox>
la source :
http://joshsmithonwpf.wordpress.com/2007/06/04/binding-to-xml/

Aucun commentaire:

Enregistrer un commentaire

Nombre total de pages vues