Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更新xmldataprovider时刷新组合框_C#_Wpf_Data Binding - Fatal编程技术网

C# 更新xmldataprovider时刷新组合框

C# 更新xmldataprovider时刷新组合框,c#,wpf,data-binding,C#,Wpf,Data Binding,我有以下代码: <Grid> <Grid.Resources> <XmlDataProvider x:Name="ScenesXmlName" x:Key="ScenesXml" XPath="scenari-list/scenario" Source="myXml.xml"/> </Grid.Resources>

我有以下代码:

<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>

组合框项目已正确加载。
我想知道的是,当我更新myXml.xml时,是否有任何方法可以更新ScenariCombo.Items(因此需要更新itemsource)。

提前谢谢

一个选项是通过
FileSystemWatcher
监视
XML
文件中的更改,并在XML文件中进行更改后更新
XmlDataProvider

我在谷歌上找到了。一个自定义的
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();
        }
    }
它将在您的场景中工作。以链接文章为例,了解更多详细信息

编辑

你可以做两件事

创建包含源文件路径的属性,并将其与XMLDataProvider的源属性绑定。一旦文件发生更改,就会引发属性更改事件,以便XML数据提供程序更新/重新加载其源(我还没有测试过这件事)

通过将源重置为同一文件,通过代码更新XMLDataProvider

话虽如此,我必须说这不是使用XML的正确方式。理想情况下,您应该在一些数据结构(如Observable collection)中加载XML,然后使用属性更改通知刷新控件中的数据。 这比预期的容易。 您必须强制重新加载xmldataprovider:

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

更新XML时是否会触发
XmlDataProvider.DataChanged
事件?是的,它会正确触发事件,但组合框不会自动更新。可以从事件处理程序中的代码再次设置组合框的源代码吗?因此,为什么不调用
ScenariCombo.ItemsSource=new XmlDataProvider(){source=new Uri(事件处理程序中的“myXml.xml”),XPath=“scenari list/scenario”};
,因为它说:“无法隐式地将类型“System.Windows.Data.XmlDataProvider”转换为“System.Collections.IEnumerable”。存在显式转换(是否缺少强制转换?)“我缺少什么转换?我必须在对xml进行更改后才能更新它,因此无需监视文件系统的更改。如何更新xmldata提供程序?我通过xaml绑定xmldatasource,但xml文件通过代码进行编辑。我想使用您建议的最后一种可能性,但我无法访问xmldataprovider表单codebehind(似乎this.ScenesXmlName不存在)。如何操作?使用
FindResource(“keyvalue”)
查找资源