使用数组C#WPF

时间:2019-01-10 15:17:46

标签: c# wpf xaml binding listbox

我已经在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();
            }
        }
    }
}

然后从那里我需要填充一个ListBox类:

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;

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

1 个答案:

答案 0 :(得分:0)

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

您也可以通过创建一个属性然后使用Binding来实现。