cosmo
Topic Author
Posts: 14
Joined: 27 Mar 2015, 13:52

SelectedItem Listbox

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?
    
<Grid>
        <ListBox SelectedItem="{Binding Item}" ItemsSource="{Binding Items}">
            
        </ListBox>
    </Grid>
    [UserControlSource("Assets/View.xaml")]
    public class MyUserControlControl : UserControl
    {
        public void OnPostInit()
        {
            DataContext = new ViewModel();
        }
    }
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));
        }
    }
 
User avatar
sfernandez
Site Admin
Posts: 1912
Joined: 22 Dec 2011, 19:20

Re: SelectedItem Listbox

24 Apr 2015, 23:33

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:
// 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; }
}
And the xaml:
<ListBox SelectedItem="{Binding Item}" ItemsSource="{Binding Items}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Value}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
Sorry for the inconveniences.
 
cosmo
Topic Author
Posts: 14
Joined: 27 Mar 2015, 13:52

Re: SelectedItem Listbox

26 Apr 2015, 01:18

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.
 
User avatar
sfernandez
Site Admin
Posts: 1912
Joined: 22 Dec 2011, 19:20

Re: SelectedItem Listbox

27 Apr 2015, 18:08

Thanks for spreading the word about NoesisGUI, we really appreciate your help ;)
 
Djekke
Posts: 4
Joined: 07 Dec 2018, 10:01

Re: SelectedItem Listbox

07 Dec 2018, 10:13

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.
Hello, I am curious, is this bug already fixed or I encountered different one?
In example below happens hard crash of application when I try to remove SelectedItem from collection.
<ListBox ItemsSource="{Binding Names}"
         SelectedItem="{Binding SelectedName}" />
public string SelectedName { get; set; }
public ObservableCollection<string> Names { get; set; }
In wpf application all works fine. If I wrap string in other class - it's works in noesis too.
 
User avatar
sfernandez
Site Admin
Posts: 1912
Joined: 22 Dec 2011, 19:20

Re: SelectedItem Listbox

07 Dec 2018, 17:43

I've tried the following code and xaml in Unity with NoesisGUI 2.1.0f1 and it worked fine:
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;
    }
}
<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>
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?
 
Djekke
Posts: 4
Joined: 07 Dec 2018, 10:01

Re: SelectedItem Listbox

07 Dec 2018, 17:58

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?
My problem happened, as i mention before, when I try to remove SelectedItem from collection for example via adding button with followed code
if (Names.Contains(SelectedName))
{
	Names.Remove(SelectedName);
}
I am not sure about crash dump as I not creating app with noesis myself, but creating mod for existing app.
 
User avatar
sfernandez
Site Admin
Posts: 1912
Joined: 22 Dec 2011, 19:20

Re: SelectedItem Listbox

07 Dec 2018, 19:58

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?
 
Djekke
Posts: 4
Joined: 07 Dec 2018, 10:01

Re: SelectedItem Listbox

07 Dec 2018, 20:15

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?
Done. https://www.noesisengine.com/bugs/view.php?id=1371
 
User avatar
sfernandez
Site Admin
Posts: 1912
Joined: 22 Dec 2011, 19:20

Re: SelectedItem Listbox

07 Dec 2018, 21:30

Thank you.

Who is online

Users browsing this forum: Google [Bot] and 1 guest