none
Blend,XAML,XML,C# binding static xml data source to listview- listview wont update even .xml is updated

    Question

  •  

    Hi,

     

    i have a problem i just bound a static .xml file to a listview with blend.

    with some buttons etc i change the .xml content and then want to be the listview updated, but it doesnt works.

    Only if i am pressing F5 the refresh button my listbox has the new items etc.

    here my XAML code:

     

    ListView DataContext="{Binding Path=SelectedItem, ElementName=Gruppenbox, Mode=OneWay}" Margin="350.324,194.529,20,27.59" IsSynchronizedWithCurrentItem="False" ItemsSource="{Binding Mode=OneWay, XPath=../Autos/Auto}" IsHitTestVisible="True" x:Name="ListViewCars" >

     

    i change a groupbox to get the different content for my listview it works great.

     

    and here my c# code (doesnt work, i inserted it after changing the .xml)

     

    ICollectionView dataView = CollectionViewSource.GetDefaultView(ListViewCars.ItemsSource);

    dataView.Refresh();

     

    any ideas?plz help me i am on headache Sadthx

     

    Thursday, March 13, 2008 4:44 PM

Answers

  • Here is something I did. XAML and CS file below. I used the FileSystemWatcher class to setup watch on my XML file and updated the XmlDataProvider when the file changed

     

    Code Snippet

    <Window x:Class="TestWPFApp.XMLTestWindow"

       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

       xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml

       Title="XMLTestWindow" Height="300" Width="300">

        <Window.Resources>

            <XmlDataProvider x:Key="xmlFromFile" Source="XMLFile1.xml" XPath="persons/person" />

            <Style x:Key="listStyle" TargetType="{x:Type ListBoxItem}">

                <Setter Property="Template">

                    <Setter.Value>

                        <ControlTemplate>

                            <StackPanel Orientation="Horizontal">

                                <TextBlock Text="{Binding XPath=@name}" />

                                <TextBlock Text="{Binding XPath=@age}" />

                            </< SPAN>StackPanel>

                        </< SPAN>ControlTemplate>

                    </< SPAN>Setter.Value>

                </< SPAN>Setter>

            </< SPAN>Style>

        </< SPAN>Window.Resources>

        <StackPanel>

            <ListBox ItemsSource="{Binding Source={StaticResource xmlFromFile}}" ItemContainerStyle="{StaticResource listStyle}" Height="70" Name="list" />

        </< SPAN>StackPanel>

    </< SPAN>Window>

     

     

    and the code behind file

     

    Code Snippet

    namespace TestWPFApp

    {

        /// <summary>

        /// Interaction logic for XMLTestWindow.xaml

        /// </summary>

        public partial class XMLTestWindow : Window

        {

            public XMLTestWindow()

            {

                InitializeComponent();

                FileSystemWatcher f = new FileSystemWatcher();

                f.Path = AppDomain.CurrentDomain.BaseDirectory; //this is where the XML file resides

                f.Filter = "XMLFile1.xml"; //name of the file i am watching

                f.Changed += new FileSystemEventHandler(f_Changed); //watch for file changed events

                f.EnableRaisingEvents = true; //without this events won't fire

            }

     

            void f_Changed(object sender, FileSystemEventArgs e)

            {

                XmlDataProvider odp = this.TryFindResource("xmlFromFile") as XmlDataProvider;

                if (odp != null)

                {

                    using (odp.DeferRefresh())

                    {

                        odp.Source = new Uri("XMLFile1.xml", UriKind.Relative);

                    }

                }

            }

        }

    }

     

     

     

     

    Tuesday, March 18, 2008 5:01 AM

All replies

  •  Tommyyy wrote:

    with some buttons etc i change the .xml content and then want to be the listview updated, but it doesnt works.

    Only if i am pressing F5 the refresh button my listbox has the new items etc.



    I think this behaviour is expected, do you expect that when you changed the xml file, the designer can intelligently detect the change, and refresh the designer surface with any user interaction? could you please elaborate on what type of "big goal" you want to achieve?

    Thanks
    Monday, March 17, 2008 7:59 AM
  • Marco is right. Designer will never do it on its own.

     

    Even at runtime the application will also not do it on its own. Some sort of trigger will need to be setup on the XML so that the application can know that it has changed and update itself.

     

    this is already handled by the framework for app.config files. If you use custom XML File you need to look at FileSystemWatcher to setup a watch on the XML and do the necessary update logic when the event fires

     

    Monday, March 17, 2008 8:33 AM
  • sure the app wont do it on its own never sayed that...

    but i just want to know how i can set the content of a view list to xml and than how to update it- code samples would be nice. i added static data to the viewlist and now i am changing the content of xml file- the direction is one way so the source is the file the target is the list view- am i right?ok now i update the file but i dont know how to update the listview,at the moment it only works by refresh,but i dont get the problem maybe it is because i set the source to a static collection and not to a dynamic one or a collection class- so the add and remoce functions from the listview.items doesnt work also - maybe because the listview displays just the content of the file and doesnt control any items in itself? i just tried to update the view after changing the file but no method worked for me i guess i do something wrong.

     

    -the listview.updatelayout doesnt work

    -the code above doesnt work - so the question i am asking myself cause i m writing my thesis on XAML- is there a way to add xml per  drag and drop without having the file static in the project ?

     

    i think about getting the file dynamically from a server and then databind it to the viewbox but i guess this wont work with blend alone or?

     

    thank you for your help 

    Monday, March 17, 2008 8:00 PM
  • Here is something I did. XAML and CS file below. I used the FileSystemWatcher class to setup watch on my XML file and updated the XmlDataProvider when the file changed

     

    Code Snippet

    <Window x:Class="TestWPFApp.XMLTestWindow"

       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

       xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml

       Title="XMLTestWindow" Height="300" Width="300">

        <Window.Resources>

            <XmlDataProvider x:Key="xmlFromFile" Source="XMLFile1.xml" XPath="persons/person" />

            <Style x:Key="listStyle" TargetType="{x:Type ListBoxItem}">

                <Setter Property="Template">

                    <Setter.Value>

                        <ControlTemplate>

                            <StackPanel Orientation="Horizontal">

                                <TextBlock Text="{Binding XPath=@name}" />

                                <TextBlock Text="{Binding XPath=@age}" />

                            </< SPAN>StackPanel>

                        </< SPAN>ControlTemplate>

                    </< SPAN>Setter.Value>

                </< SPAN>Setter>

            </< SPAN>Style>

        </< SPAN>Window.Resources>

        <StackPanel>

            <ListBox ItemsSource="{Binding Source={StaticResource xmlFromFile}}" ItemContainerStyle="{StaticResource listStyle}" Height="70" Name="list" />

        </< SPAN>StackPanel>

    </< SPAN>Window>

     

     

    and the code behind file

     

    Code Snippet

    namespace TestWPFApp

    {

        /// <summary>

        /// Interaction logic for XMLTestWindow.xaml

        /// </summary>

        public partial class XMLTestWindow : Window

        {

            public XMLTestWindow()

            {

                InitializeComponent();

                FileSystemWatcher f = new FileSystemWatcher();

                f.Path = AppDomain.CurrentDomain.BaseDirectory; //this is where the XML file resides

                f.Filter = "XMLFile1.xml"; //name of the file i am watching

                f.Changed += new FileSystemEventHandler(f_Changed); //watch for file changed events

                f.EnableRaisingEvents = true; //without this events won't fire

            }

     

            void f_Changed(object sender, FileSystemEventArgs e)

            {

                XmlDataProvider odp = this.TryFindResource("xmlFromFile") as XmlDataProvider;

                if (odp != null)

                {

                    using (odp.DeferRefresh())

                    {

                        odp.Source = new Uri("XMLFile1.xml", UriKind.Relative);

                    }

                }

            }

        }

    }

     

     

     

     

    Tuesday, March 18, 2008 5:01 AM
  • thank you- that is what i am looking for.

    Tuesday, March 18, 2008 7:45 AM
  • Earlier today, i published a blog on this with a slightly different approach. Publishing the URL here so that others may also benefit - http://infosysblogs.com/microsoft/2008/03/wpf_updating_xmldataprovider_w.html

    Wednesday, March 26, 2008 6:16 AM
  •  

    maybe im doing something wrong, but when i update my xml file, ListView is not updated.

    besides what is shown, do i need to do anything else?

    Wednesday, April 02, 2008 4:05 PM
  •  

    Anyone please?
    Wednesday, April 02, 2008 10:32 PM
  • can you debug and see if the file system watcher event is being fired or not when you modify the XML file?

     

    Thursday, April 03, 2008 1:54 PM
  • i put a breakpoint and its not being fired

    but the files is modified...

    Thursday, April 03, 2008 2:16 PM
  • breakpoint not firing will mean that either the Path or Filter is incorrectly set. Check on that again. The Path should point to the folder where the Xml is residing and the filter to the actual XML file name.

     

    What happens if you close your application, change the XML and then re-start the application? (outside of Visual Studio, directly using the EXE). Does it pick up the new values in the XML?

    Friday, April 04, 2008 3:32 AM