code_junkie

Can you bind the selected item in a listbox to a separate object in WPF?

Nov 12th, 2011
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. public class MyDataList
  2. {
  3. public ObservableCollection<Guid> Data { get; set; }
  4. }
  5.  
  6. public class MyDataStructure
  7. {
  8. public Guid ChosenItem { get; set; }
  9. }
  10.  
  11. <Window.Resources>
  12. <local:MyDataStructure x:Key="mds1" />
  13. </Window.Resources>
  14. <ListBox ItemsSource="{Binding Data}" SelectedValue="{Binding Source={StaticResource mds1} Path=ChosenItem}"/>
  15.  
  16. public class MyDataList : INotifyPropertyChanged
  17. {
  18. private Guid _choosen;
  19.  
  20. public ObservableCollection<Guid> Data { get; set; }
  21.  
  22. public Guid ChosenItem {
  23. get
  24. {
  25. return _choosen;
  26. }
  27. set
  28. {
  29. _choosen = value;
  30. PropertyChanged(this, new PropertyChangedEventArgs("ChosenItem"));
  31. }
  32. }
  33.  
  34. public event PropertyChangedEventHandler PropertyChanged;
  35. }
  36.  
  37. <ListBox ItemsSource="{Binding Data}" SelectedValue="{Binding Path=ChosenItem}" x:Name="listBox"/>
Add Comment
Please, Sign In to add comment