0

I am new to WPF. I have a listBox with various elements graphics elements in it. The element in the listBox are linked to a list.

enter image description here

At the moment to add elements I am doing it the old way that is with no binding:

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

but I know I'm doing it wrong, because I'll have to manually handle deletion, add, reorder of element and so on. I Would like to be able to bind the elements in the listBox to the elements of the following class:

[Serializable]
public class EasyRunXmlSerializableData
{
    public EasyRunXmlSerializableData()
    { }

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

with

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

    ....

thanx for any help Patrick

1
  • 1
    As asked, the question is too broad. The short version is that you need to learn about data templating, create a view model class that contains the data that will control the visual appearance of a list item and then in XAML declare a DataTemplate for your view model type (or types) that bind to the appropriate properties in the view model class and present the desired appearance according to the values in those properties. Commented Nov 22, 2015 at 22:27

1 Answer 1

1

Following links should get you started in the right direction.

You need to understand DataTemplate, and Data Binding for your current scenario.

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 Links

DataBinding - How to

DataBinding - FAQ

Scott's DataBinding tutorial

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.