SelectedItem Listbox
Posted: 24 Apr 2015, 19:49
Hello,
I am unable to get the following to to function. What I would like to do is have a listbox. Bind that listbox to a list of strings and when a listbox item is selected have the selection be set on the viewmodel.
Here is a relevant example of the code that I am attempting to get to work. This does work in a wpf application and seems to be a fairly simple use case.
I am getting my three items in the listbox but the selected item is not being set on the view model when the listbox item is clicked.
What should I be doing differently to make this work in NoesisGUI?
I am unable to get the following to to function. What I would like to do is have a listbox. Bind that listbox to a list of strings and when a listbox item is selected have the selection be set on the viewmodel.
Here is a relevant example of the code that I am attempting to get to work. This does work in a wpf application and seems to be a fairly simple use case.
I am getting my three items in the listbox but the selected item is not being set on the view model when the listbox item is clicked.
What should I be doing differently to make this work in NoesisGUI?
Code: Select all
<Grid>
<ListBox SelectedItem="{Binding Item}" ItemsSource="{Binding Items}">
</ListBox>
</Grid>
Code: Select all
[UserControlSource("Assets/View.xaml")]
public class MyUserControlControl : UserControl
{
public void OnPostInit()
{
DataContext = new ViewModel();
}
}
Code: Select all
public class ViewModel : INotifyPropertyChanged
{
private List<string> _items;
private string _selectedItem;
public ViewModel()
{
Items = new List<string>{"One","Two","Three"};
}
public string Item
{
get
{
//This code is getting called constantly.
return _selectedItem;
}
set
{
//This code is never being hit.
_selectedItem = value;
OnPropertyChanged("Item");
}
}
public List<string> Items
{
get { return _items; }
set
{
_items = value;
OnPropertyChanged("Items");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}