I don't know if I would be informative enough, but I'm having a problem. I bound an ObservableCollection to a normal Listbox, everything is working fine, but ImageInfo has a member (Source) which contains the place where the image is, and I need the Source member of the current selected item in the Listbox. However, I don't seem to have a clue where to start.
-
1the question is unclear, please add more details – Arsen Mkrtchyan Oct 17 '13 at 20:43
-
You mean, you have ImageInfo class, which have source property, and you want to bind to it? – Arsen Mkrtchyan Oct 17 '13 at 20:44
-
Bind SelectedItem to ViewModel's property. – dev hedgehog Oct 17 '13 at 20:55
2 Answers
Maybe you need in your xaml something like <Image Source="{Binding ElementName=myListbox, Path=SelectedItem.Source}">
. Other examples and explanations related to binding here https://stackoverflow.com/a/1069389/1606534

- 1
- 1

- 920
- 1
- 13
- 22
-
Thank you, I just didn't think I could use "." in "SelectedItem.Source" to access the members – Imad Nabil Alnatsheh Oct 17 '13 at 21:35
Are you binding in normal mode to a property like: EG: < combobox itemssource={Binding Listing}/>? If so you really just need to have a public property exposed for the 'selecteditem' if memory serves. The real power in Observable Collection from my understanding of WPF is how things can change in real time and you can notice those changes when implementing INotifyPropertyChanged or INotifyCollectionChanged.
<combobox x:Name="mycombo" itemssource="{Binding itemsource}"
selecteditem="{Binding SelectedItem}" />
ViewModel property:
public string SelectedItem { get; set; }
However if you want your property to be noticed when it changes you need to implement INotifyPropertyChanged. Typically then in studios I have worked in they set a private variable at the top of the class and then use it in the get set and then use the public property in bindings.
public class example : INotifyPropertyChanged
{
private string _SelectedItem;
public string SelectedItem
{
get { return _SelectedItem; }
set
{
_SelectedItem = value;
RaisePropertyChanged("SelectedItem");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
public void DoSomething()
{
Messagebox.Show("I selected: " + SelectedItem);
}
}

- 14,131
- 10
- 56
- 94