.net listbox的编程绑定在WPF中不起作用

.net listbox的编程绑定在WPF中不起作用,.net,wpf,listbox,datatemplate,xmldataprovider,.net,Wpf,Listbox,Datatemplate,Xmldataprovider,我有一个用户控件,它以编程方式设置listbox的数据源 确切地说,是XmlDataProvider和DataTemplate,但在运行时它从未正确显示。加载用户控件时。数据提供程序的所有设置均未反映 你能帮我做这个吗? 我在开发WPF应用程序方面是个新手 短暂性脑缺血发作 代码如下: XAML 这是.cs文件 using System; using System.Collections.Generic; using System.Linq; using System.Text; using S

我有一个用户控件,它以编程方式设置listbox的数据源 确切地说,是XmlDataProvider和DataTemplate,但在运行时它从未正确显示。加载用户控件时。数据提供程序的所有设置均未反映

你能帮我做这个吗? 我在开发WPF应用程序方面是个新手

短暂性脑缺血发作

代码如下:

XAML

这是.cs文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;

using EngagiaCL.CommonObjects;
using EngagiaCL.Functions;

namespace ENGAGIAUCL.Views
{
    /// <summary>
    /// Interaction logic for ImageViewer.xaml
    /// </summary>
    public partial class ImageViewer : UserControl
    {
        public ImageViewer()
        {
            InitializeComponent();
            Loaded += (s, e) =>
                {
                    LoadContents();
                };
        }

        #region Methods
        private void LoadContents()
        {
            if (CurrentUser != null)
            {
                XmlDataProvider provider = (XmlDataProvider)this.FindResource("FormDataProvider");
                DataTemplate template = (DataTemplate)this.FindResource("FormTemplate");
                Binding templatebinding = new Binding();

                provider.Document = CurrentUser.UserDoc;
                provider.XPath = GetResourcePath();

                template.DataType = (object)GetDataTemplateObject();
                Resources["FormDataProvider"] = provider;
                Resources["FormTemplate"] = template;
            }
        }
        private string GetResourcePath()
        {
            string path = string.Empty;

            if (ContentType == "ADMIN")
            {
                path = "/SyncLoginResponse/AdminForms/AdminForm";
            }
            else
            {
                path = "/SyncLoginResponse/Forms/Form";
            }

            return path;
        }
        private string GetDataTemplateObject()
        {
            string templateobject = string.Empty;

            if (ContentType == "ADMIN")
            {
                templateobject = "AdminForm";
            }
            else
            {
                templateobject = "Form";
            }

            return templateobject;
        }
        #endregion

        #region Properties
        public UserInformation CurrentUser { get; set; }
        public string ContentType { get; set; }
        #endregion
    }
}
以下是供参考的xml:

</SyncLoginResponse>
    <AdminForms>
        <AdminForm>
            <name>Best Form Ever/html</name>
            <url>
                http://blahblahblah/
            </url>
        </AdminForm>
    </AdminForms>
</SyncLoginResponse>
注意事项:

CurrentUser是在UserDoc属性中包含xml文档的对象。 我在这个应用程序中所做的大部分工作都是我在谷歌搜索中所理解的零碎内容,所以请耐心等待。
老实说,我无法理解你的是不是一个新的加载WPF用户界面的编码实践,但很少有事情看起来像是一个失误

您的ListBox.ItemsSource被绑定为{Binding},即绑定到整个视图的DataContext,但在您的代码中,您没有设置视图的DataContext或ListBox的任何更高级别父操作系统

创建后,代码隐藏中的templateBinding变量不会在任何地方使用

为什么要在LoadContents方法的末尾再次设置FormDataProvider和FormTemplate?如果您想刷新资源字典,那么这不是正确的方法

要刷新资源,您需要删除和添加资源字典中的资源,同时使用DynamicSource标记引用


请告诉我您到底想要实现什么。

谢谢您的回复,如果我的代码比较混乱,请原谅。我最初是这样编码FormDataProvider的资源值的:XmlDataProviderResources[FormDataProvider].Document=CurrentUser.UserDoc;但这不起作用。我希望发生的是:1。加载用户控件后,根据我提供给它的xml流提要,我将能够以编程方式定义其XmlDocument和XPath。例如:如果xml来自管理员,XPath将指向可用的AdminForms节点2。将结果绑定到列表框,并将选定的列表项显示到框架中。谢谢
</SyncLoginResponse>
    <AdminForms>
        <AdminForm>
            <name>Best Form Ever/html</name>
            <url>
                http://blahblahblah/
            </url>
        </AdminForm>
    </AdminForms>
</SyncLoginResponse>