I’ve searched google and cannot find any answers to the particular problem.
I have a listbox in my form which has got some custom objects in them.
foreach (Fixture fixture in FixtureLibrary)
{
if (fixture.ModelName == "")
{
//Nothing
}
else
{
lbxLibrary.Items.Add(fixture);
}
}
In the listbox I would like to see the ModelName property. I can do this by changing the following:
lbxLibrary.Items.Add(fixture.ModelName);
But I need to be able to select an object at runtime from the list, so this approach wont work for me.
Anybody got any ideas, all I’ve found is for winforms but that doesn’t really help me as I’m using WPF.
Cheers chaps
Mike.
You should do it like this:
Create a ViewModel class with these properties, it should mediate between the UI and your business logic. Assign the ViewModel as the DataContext of your UserControl/Window.
File: FixtureViewModel.cs
FixtureUserControl.cs
Then just assign your list of Fixtures somewhere in your ViewModel-Code.
You can then databind it in WPF. Create a DataTemplate like this and put it in your UserControls Resources or a ResourceLibrary:
Notice the DataType attribute. You probably need to define the namespace for your Fixture object.
And databind your list like this:
Then you can always access the SelectedFixture Object from anywhere in the ViewModel or even the UserControl, if you really need to.