具有绑定到字符串列表的各种元素的 c# listBox

c# listBox with various elements binded to a list of strings

我是 WPF 新手。我有一个包含各种元素图形元素的列表框。 listBox 中的元素链接到一个列表。

目前要添加元素,我使用的是没有绑定的旧方法:

StackPanel sp = new StackPanel();
string currentDir = AppDomain.CurrentDomain.BaseDirectory.ToString();
TextBox tb = new TextBox()
{
    Text = strContent,
    BorderBrush = new SolidColorBrush(Colors.Gainsboro),
    IsReadOnly = true,
    ToolTip = strNotes,
    FontSize = 12,
    FontWeight = FontWeights.Bold,
    Width = IMAGES_ROW_HEIGHT,
    Height = IMAGES_ROW_HEIGHT / GOLDEN_RATIO,
    Background = null,
    Margin = new Thickness(BUTTON_MARGIN),
    VerticalContentAlignment = VerticalAlignment.Center,
    HorizontalContentAlignment = HorizontalAlignment.Center
};
sp.Children.Add(tb);
Image newResizedImage = ImageUtilities.StrPath2ResizedImageSizeHeight(strPathImage, IMAGES_ROW_HEIGHT);
if (newResizedImage != null)
{
    sp.Children.Add(newResizedImage);
    sp.Orientation = Orientation.Horizontal;
    sp.HorizontalAlignment = HorizontalAlignment.Left;
}
lbxPPgroups.Items.Add(sp);
lbxPPgroups.SelectedIndex = 0;



var newGroup = new PcDmisData.Group();
newGroup.Description = strContent;
var newImage = new PcDmisData.MyImage();
newImage.Image = newResizedImage;
newImage.IsImageEmbedded = false;

newGroup.myImage = newImage;
newGroup.Notes = strNotes;
easyRunData.olstPPgroups.Add(newGroup);

但我知道我做错了,因为我必须手动处理元素的删除、添加、重新排序等等。 我希望能够将 listBox 中的元素绑定到以下 class:

的元素
[Serializable]
public class EasyRunXmlSerializableData
{
    public EasyRunXmlSerializableData()
    { }

    //PcDmis Data           
    public ObservableCollection<PcDmisData.Group> olstPPgroups = new ObservableCollection<PcDmisData.Group>();  
}

public class PcDmisData
{       
    [Serializable]
    public class Group
    {
        public string Description;<---------this for the text of the textbox
        public MyImage myImage;<------------this is the image    
        public string Notes;<---------------this for a tooltip
        public ObservableCollection<PartProgram> partProgramList = new ObservableCollection<PartProgram>();
   }


        [Serializable]
        public class MyImage
        {
            public object Image;
            public bool IsImageEmbedded;
        }

    ....

感谢您的帮助 帕特里克

以下链接应该会让您朝着正确的方向开始。

您需要了解当前场景的 DataTemplate 和数据绑定。

MSDN : How to display data in a ListBox

ListBox tutorial basics

ListBox Custom Layout

How to get a ListBoxItem from a data bound ListBox

数据绑定链接

DataBinding - How to

DataBinding - FAQ

Scott's DataBinding tutorial