Ryan Dawson on Longhorn

The software we think, but do not write

January 2004 - Posts

  • XmlDataSource

    Filed under:

    Feature of the week: XmlDataSource


    Seriously, this is a feature you have to take a look at.  There are all types of inherent features built-in, such as processing XSD, XSLT, and Caching.  Oh yeah, and don’t forget Xpath expressions, something that I literally work with on a daily basis.  The beauty is that it can easily bind to a Control, and therefore it is trivial to store data in an XML file and populate user controls.


    Here is a quick rundown of the implementation:

    • Define an XmlDataSource element within your resources section.  Within my XmlDataSource I have hard-coded the XML, but persisting from disk is trivial.



              <XmlDataSource def:Name="Models">                

                  <Models xmlns="">

                    <Model Name="Heidi Klum" Country="USA" />

                    <Model Name="Gisele Bundchen" Country="Brazil" />

                    <Model Name="Molly Sims" Country="USA" />

                    <Model Name="Petra Nemcova" Country="Czech Republic" />

                    <Model Name="Daniela Pestova"

    Country="Czech Republic" />





    • Next we need to declare 2 controls, one is going to control our Xpath expression, and the other is going to display the result.  In my case, I have used the ListBox control.


          <FlowPanel DockPanel.Dock="Fill">

             <ListBox SelectionChanged="OnCountryChanged">



    <ListItem>Czech Republic</ListItem>


             <ListBox ItemStyle="{ModelItemStyle}">




                            <Bind DataSource="{Models}" />







                There are 3 interesting lines:

    1.      SelectionChanged="OnCountryChanged"

    The SelectionChanged Event on our first list box will give us programmatic control when an item is selected.  This will allow us to create an Xpath that will modify the second ListBox.  Here is the C# code:


    private void OnCountryChanged(object sender, SelectionChangedEventArgs args)


                      if (args.SelectedItems.Count > 0)


    ListItem item = args.SelectedItems[0] as ListItem;

    string query = string.Format("/Models/Model[@Country='{0}']", item.Content);


    XmlDataSource models = (XmlDataSource)this.FindResource("Models");

                            models.XPath = query;





    Basically, we grab an instance of the ListItem clicked, and take the Text value to feed into our Xpath expression, which will select all models that have an attribute Country of the same value.  We then grab an instance of our XmlDataSource with the FindResource method and change the Xpath.  Refresh it and you are done.


    2.      <Bind DataSource="{Models}" />

    Indeed, we have left out the most trivial line of code, but the guts of the operation.  This will lock in our XmlDataSource as the DataSource for the ListBox.


    3.      ItemStyle="{ModelItemStyle}"

    This line goes in hand with the following code, which should be placed within your Resource declaration as the XmlDataSource:


                <Style def:Name="ModelItemStyle">


                            <SimpleText Text="*Bind(Path=@Name)"  />




    It seals the deal by binding the Name attribute of the Model element in the XML source to the Text property of the SimpleText.  This is analogous to overriding the ToString() method within a class in C#.


    The XmlDataSource is elegant, there is no question.  I think it will come in real handy when working with XML at the control level, such as in the example.


  • System Stats Using Avalon Animation Lingo Article

    Filed under:

    This sample is intended to show the “wow” effect for Avalon, and still maintain a sense of functionality.  We will build a Task Manager in the most basic form, using many types of Animation –RotateTransform, TranslateTransform, and TransformDecorator Animations for properties such as Opacity, Width, and Height.


    The Article can be found here


    Source Code