简体   繁体   English

xmldataprovider更新时刷新组合框

[英]refresh combobox when xmldataprovider is updated

i have this code: 我有此代码:

<Grid>
        <Grid.Resources>
            <XmlDataProvider x:Name="ScenesXmlName" x:Key="ScenesXml"
                    XPath="scenari-list/scenario"
                    Source="myXml.xml"/>
        </Grid.Resources>

        <ComboBox Name="ScenariCombo"
                  ItemsSource="{Binding Source={StaticResource ScenesXml}}"
                  DisplayMemberPath="@name"
                  SelectionChanged="ScenariCombo_SelectionChanged" />
</Grid>

Combobox items are loaded correctly. 组合框项目已正确加载。
What i wanto to know is if there is any way to update ScenariCombo.Items when i update myXml.xml (so the itemsource). 我想知道的是,当我更新myXml.xml(因此itemsource)时,是否有任何方法可以更新ScenariCombo.Items。
Thanks in advance! 提前致谢!

One option could be to watch for changes in XML file through FileSystemWatcher and update the XmlDataProvider once changes are made in XML file. 一种选择是通过FileSystemWatcher监视XML文件中的更改,并在XML文件中进行更改后更新XmlDataProvider

On little googling I found this . 在小小的谷歌搜索中,我发现了这一点 A custom XMLDataProvider with built-in functionality of looking for any changes in XML file through FileSystemWatcher . 具有内置功能的自定义XMLDataProvider ,可以通过FileSystemWatcher查找XML文件中的任何更改。

public class MyXmlDataProvider : XmlDataProvider
    {
        public new Uri Source
        {
            get { return base.Source; }
            set
            {
                base.Source = value;

                FileSystemWatcher watcher = new FileSystemWatcher();
                //set the path of the XML file appropriately as per your requirements
                watcher.Path = AppDomain.CurrentDomain.BaseDirectory;

                //name of the file i am watching
                watcher.Filter = value.OriginalString;

                //watch for file changed events so that we can refresh the data provider
                watcher.Changed += new FileSystemEventHandler(file_Changed);

                //finally, don't forget to enable watching, else the events won't fire           
                watcher.EnableRaisingEvents = true;
            }
        }

        void file_Changed(object sender, FileSystemEventArgs e)
        {
            base.Refresh();
        }
    }

It will work in your scenario. 它将在您的情况下工作。 Take a look at linked article for example and more details 以链接文章为例,以及更多详细信息

Edit 编辑

You can do two things. 您可以做两件事。

Either create a property containing source file path and bind it with Source property of XMLDataProvider. 创建一个包含源文件路径的属性,并将其与XMLDataProvider的Source属性绑定。 Once the file gets changes raise a property changed event so that XML Data provider updates/reloads its source (I haven't tested this thing) 文件更改后,引发一个属性更改事件,以便XML数据提供程序更新/重新加载其源(我尚未测试过此东西)

Or 要么

Update the XMLDataProvider through code by resetting the source to same file. 通过将源重置为同一文件,通过代码更新XMLDataProvider。

Having said that I must say this is not the right way of playing with XML. 话虽如此,我必须说这不是使用XML的正确方法。 Ideally you should load XML in some data structure like Observable collection then use the property changed notification to refresh the data in your control 理想情况下,您应该将XML加载到某些数据结构(如Observable集合)中,然后使用属性更改通知来刷新控件中的数据

I finally understood this. 我终于明白了。 It was easier than expected. 这比预期的要容易。 You have to force the reloading of the xmldataprovider: 您必须强制重新加载xmldataprovider:

XmlDataProvider xmlDataProvider = (XmlDataProvider)BaseGrid.FindResource("ScenesXml");
xmlDataProvider.Refresh();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM