Reputation: 2593
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.
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
Upvotes: 0
Views: 62
Reputation: 9827
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
How to get a ListBoxItem from a data bound ListBox
DataBinding Links
Upvotes: 1