使用数组 C# WPF 中的项目填充列表框

Populating Listbox with items from an array C# WPF

我在 xaml 文件中创建了一个字符串数组,我需要将其用作 c# wpf ListBox 控件中的项目。我尝试了各种方法从数组中获取项目以添加到 ListBox,但无济于事。这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <string>DocumentSettings.DepositRuntimeDefaults</string>
  <string>DocumentSettings.LendingCustomization.CommonSettings</string>
</ArrayOfString>

这就是数组,现在在我后面的代码中,我有一个创建列表的单例:

using System.Collections.Generic;
using System.IO;

namespace csi.Framework.Business
{
    public class UIPathOptionsManager
    {
        public static UIPathOptionsManager Instance = new UIPathOptionsManager();

        public List<string> UIPathOptions;

        public string theUIPathOptionsFile { get; set; }

        public void Initialize(string theDirectory)
        {
            theUIPathOptionsFile = theDirectory + "\UIPathOptions.xaml";
            if (File.Exists(theUIPathOptionsFile))
            {
                System.Xml.Serialization.XmlSerializer xmlDeserializer = new
                System.Xml.Serialization.XmlSerializer(typeof(List<string>));
                TextReader fileReader = new StreamReader(theUIPathOptionsFile);
                UIPathOptions = (List<string>)xmlDeserializer.Deserialize(fileReader);
                fileReader.Close();
            }
        }
    }
}

从那里我有一个需要填充的列表框 class:

ListBox theUIPathOptionslistBox = new ListBox();
                theUIPathOptionslistBox.Items.Add();
                theUIPathOptionslistBox.TabIndex = nRow;
                theUIPathOptionslistBox.SelectionMode = SelectionMode.Multiple;
                theUIPathOptionslistBox.ClipToBounds = true;
                theUIPathOptionslistBox.Focusable = true;
                theUIPathOptionslistBox.Visibility = Visibility.Hidden;
                theUIPathOptionslistBox.Height = 24;

我真的希望有人能帮助我 - 感觉我应该知道这一点但是....

将 UIPathOptionsManager.Instance.UIPathOptions 分配给 ItemSource。在 Window_Loaded 事件中执行此操作。

UIPathOptionsManager.Instance.Initialize
(@"C:\Users\Administrator\source\repos\WpfApp9");

        ListBox theUIPathOptionslistBox = new ListBox();


        theUIPathOptionslistBox.ItemsSource = UIPathOptionsManager.Instance.UIPathOptions;


        theUIPathOptionslistBox.TabIndex = nRow;
        theUIPathOptionslistBox.SelectionMode = SelectionMode.Multiple;
        theUIPathOptionslistBox.ClipToBounds = true;
        theUIPathOptionslistBox.Focusable = true;
        theUIPathOptionslistBox.Visibility = Visibility.Hidden;
        theUIPathOptionslistBox.Height = 24;

您也可以通过创建 属性 然后使用绑定来实现。