SelectedItem Listbox
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));
}
}
-
-
sfernandez
Site Admin
- Posts: 3203
- Joined:
Re: SelectedItem Listbox
Hi,
I just tried your example and confirmed it is a bug in our side. The problem is that the items of the list, as they are basic types, are boxed when transferred to the native world, and each time we ask about an item, a different boxed object is created in C++, so selection fails to find the item in the list.
Please report the bug in our bugtracker to follow its status.
While we find a way to solve this issue, your only option is to use a class for the items and use an ItemTemplate to show the item value, something like this:
And the xaml:
Sorry for the inconveniences.
I just tried your example and confirmed it is a bug in our side. The problem is that the items of the list, as they are basic types, are boxed when transferred to the native world, and each time we ask about an item, a different boxed object is created in C++, so selection fails to find the item in the list.
Please report the bug in our bugtracker to follow its status.
While we find a way to solve this issue, your only option is to use a class for the items and use an ItemTemplate to show the item value, something like this:
Code: Select all
// Base class for all property change notification classes
public class NotifyPropertyChangedBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
// I made this wrapper class using generics so it can be used for any basic type
public class Val<T> : NotifyPropertyChangedBase
{
public Val(T t) { Value = t; }
public static implicit operator T(Val<T> v) { return v.Value; }
public static implicit operator Val<T>(T t) { return new Val<T>(t); }
public override string ToString() { return Value.ToString(); }
T _value;
public T Value
{
get { return _value; }
set { _value = value; OnPropertyChanged("Value"); }
}
}
// Your ViewModel
public class ViewModel : NotifyPropertyChangedBase
{
private Val<string> _selectedItem;
public ViewModel()
{
Items = new List<Val<string>> { "One", "Two", "Three" };
}
public Val<string> Item
{
get { return _selectedItem; }
set
{
_selectedItem = value;
OnPropertyChanged("Item");
}
}
public List<Val<string>> Items { get; private set; }
}
Code: Select all
<ListBox SelectedItem="{Binding Item}" ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Value}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Re: SelectedItem Listbox
Thank you for the solution.
Although things are not 100% yet i think the small upfront investment in NoesisGUI will save me a lot of time in the long run and provide a beautiful looking user interface for customers.
I'll be at build next week and will mention your product to anyone I see there doing Unity development.
Although things are not 100% yet i think the small upfront investment in NoesisGUI will save me a lot of time in the long run and provide a beautiful looking user interface for customers.
I'll be at build next week and will mention your product to anyone I see there doing Unity development.
-
-
sfernandez
Site Admin
- Posts: 3203
- Joined:
Re: SelectedItem Listbox
Thanks for spreading the word about NoesisGUI, we really appreciate your help 

Re: SelectedItem Listbox
Hello, I am curious, is this bug already fixed or I encountered different one?Hi,
I just tried your example and confirmed it is a bug in our side. The problem is that the items of the list, as they are basic types, are boxed when transferred to the native world, and each time we ask about an item, a different boxed object is created in C++, so selection fails to find the item in the list.
Please report the bug in our bugtracker to follow its status.
In example below happens hard crash of application when I try to remove SelectedItem from collection.
Code: Select all
<ListBox ItemsSource="{Binding Names}"
SelectedItem="{Binding SelectedName}" />
Code: Select all
public string SelectedName { get; set; }
public ObservableCollection<string> Names { get; set; }
-
-
sfernandez
Site Admin
- Posts: 3203
- Joined:
Re: SelectedItem Listbox
I've tried the following code and xaml in Unity with NoesisGUI 2.1.0f1 and it worked fine:
Items get selected and a message gets written to the Unity console.
If you get a crash, could you please create a ticket in our bugtracker and attach a crash dump so we can find what failed?
Code: Select all
public class ViewModel
{
private string _selectedName;
public string SelectedName
{
get { return _selectedName; }
set
{
if (_selectedName != value)
{
_selectedName = value;
UnityEngine.Debug.Log("Selected=" + _selectedName);
}
}
}
public ObservableCollection<string> Names { get; set; }
}
public class TestBehavior : MonoBehaviour
{
private void Start()
{
NoesisView view = GetComponent<NoesisView>();
ViewModel vm = new ViewModel();
vm.Names = new ObservableCollection<string>()
{
"First", "Second", "Third", "Fouth"
};
view.Content.DataContext = vm;
}
}
Code: Select all
<Grid
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ListBox Width="300" Height="300"
ItemsSource="{Binding Names}"
SelectedItem="{Binding SelectedName}" />
</Grid>
If you get a crash, could you please create a ticket in our bugtracker and attach a crash dump so we can find what failed?
Re: SelectedItem Listbox
My problem happened, as i mention before, when I try to remove SelectedItem from collection for example via adding button with followed codeI've tried the following code and xaml in Unity with NoesisGUI 2.1.0f1 and it worked fine:
Items get selected and a message gets written to the Unity console.
If you get a crash, could you please create a ticket in our bugtracker and attach a crash dump so we can find what failed?
Code: Select all
if (Names.Contains(SelectedName))
{
Names.Remove(SelectedName);
}
-
-
sfernandez
Site Admin
- Posts: 3203
- Joined:
Re: SelectedItem Listbox
Ok, I was able to reproduce the crash with NoesisGUI 2.1.0f1.
Can you please report it in our bugtracker so we can solve it for the upcoming 2.2 version?
Can you please report it in our bugtracker so we can solve it for the upcoming 2.2 version?
Re: SelectedItem Listbox
Done. https://www.noesisengine.com/bugs/view.php?id=1371Ok, I was able to reproduce the crash with NoesisGUI 2.1.0f1.
Can you please report it in our bugtracker so we can solve it for the upcoming 2.2 version?
Who is online
Users browsing this forum: Ahrefs [Bot] and 8 guests