User avatar
Scherub
Topic Author
Posts: 141
Joined: 06 May 2014, 20:53
Contact:

[Unity] Issues with binding controls to a view model

08 May 2014, 00:15

Hello.

I have a few questions regarding binding of controls as I stumbled over a few issues yesterday. But before I start I should probably mention that I use noesisGUI within Unity.

As I said I was trying out the binding of a few controls to a view model. It worked well with textblocks, textboxes and even binding to a command on a button worked without any problems. My first try binding the value property of a progressbar failed on the other hand. However, after explicitly telling it that's a one-way-binding it worked. Is there a reason why I have to explicitily state it?

After that I tried to bind a slider but I didn't get that one to work at all. In one of the sameples I saw that you could bind the value to another framework element, but appearantly it doesn't seem work with a view model.
Did I do something wrong, is it a bug or just not supported right now? Is there an overview somewhere what bindings are already supported?

I also tried to fill a listbox but that didn't work with binding too. So I had to fill it manually to get it to work. What type of collection should I use for itemsource controls or does the binding doesn't work at all? The listview seems to have the same problem.

Somewhere on your forums I read that ValueConverters aren't supported within Unity. But that was a few months ago. Has that changed already or is there an estimated release date?
 
User avatar
sfernandez
Site Admin
Posts: 1915
Joined: 22 Dec 2011, 19:20

Re: [Unity] Issues with binding controls to a view model

08 May 2014, 13:09

My first try binding the value property of a progressbar failed on the other hand. However, after explicitly telling it that's a one-way-binding it worked. Is there a reason why I have to explicitly state it?
I made a simple test with a view model and a ProgressBar and had no problem with the default TwoWay binding. It shouldn't be necessary to change the Binding Mode to OneWay, unless this is what you exactly want. Could you please report the issue in our bugtracker (bugs.noesisengine.com) and attach the xaml file and view model code? My code looks like this:
<Grid
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid.DataContext>
        <ViewModel Val="20"/>
    </Grid.DataContext>
    <ProgressBar Value="{Binding Val}" Width="200" Height="20" VerticalAlignment="Center" Minimum="0" Maximum="100"/>
    <Button x:Name="btn" Content="Button" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="10" Padding="20,2"/>
</Grid>
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[Noesis.Extended]
public class ViewModel : Noesis.SerializableComponent
{
    private float _val = 0.0f;
    public float Val
    {
        get
        {
            return this._val;
        }
        set
        {
            if (this._val != value)
            {
                this._val = value;
                NotifyPropertyChanged("Val");
            }
        }
    }
}

public class TestBehavior : MonoBehaviour
{
    ViewModel _viewModel;

    // Use this for initialization
    void Start()
    {
        var gui = GetComponent<NoesisGUIPanel>();
        var root = gui.GetRoot<Noesis.FrameworkElement>();
        var btn = root.FindName<Noesis.Button>("btn");
        btn.Click += this.OnBtnClick;

        this._viewModel = root.GetDataContext().As<ViewModel>();
    }

    void OnBtnClick(Noesis.BaseComponent sender, Noesis.RoutedEventArgs e)
    {
        float val = (Time.frameCount % 1000) / 10.0f;
        this._viewModel.Val = val;
    }
}
After that I tried to bind a slider but I didn't get that one to work at all. In one of the sameples I saw that you could bind the value to another framework element, but appearantly it doesn't seem work with a view model.
Did I do something wrong, is it a bug or just not supported right now? Is there an overview somewhere what bindings are already supported?
I had no problems neither if I use my previous test changing the ProgressBar to a Slider. It must be a bug in our side, so I suggest again to report it in the bugtracker. You can find more info about bindings here and here.
I also tried to fill a listbox but that didn't work with binding too. So I had to fill it manually to get it to work. What type of collection should I use for itemsource controls or does the binding doesn't work at all? The listview seems to have the same problem.
Our API provides a custom list with change notifications already implemented, the Collection class. You have to use this one by now (in a future we would like to improve this and support .net default array and containers). Your view model can expose a property of this type and use it in the xaml:
<Grid
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid.DataContext>
        <ViewModel Val="20"/>
    </Grid.DataContext>
    <ListBox ItemsSource="{Binding Items}" Width="100" Height="100"/>
</Grid>
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[Noesis.Extended]
public class ViewModel : Noesis.SerializableComponent
{
    public Noesis.Collection Items { get; private set; }

    public ViewModel()
    {
        Items = new Noesis.Collection();
    }
}

public class TestBehavior : MonoBehaviour
{
    ViewModel _viewModel;

    // Use this for initialization
    void Start()
    {
        var gui = GetComponent<NoesisGUIPanel>();
        var root = gui.GetRoot<Noesis.FrameworkElement>();

        this._viewModel = root.GetDataContext().As<ViewModel>();
        this._viewModel.Items.Add("item 1");
        this._viewModel.Items.Add("item 2");
        this._viewModel.Items.Add("item 3");
        this._viewModel.Items.Add("item 4");
        this._viewModel.Items.Add("item 5");
        this._viewModel.Items.Add("item 6");
        this._viewModel.Items.Add("item 7");
        this._viewModel.Items.Add("item 8");
        this._viewModel.Items.Add("item 9");
    }
}
Somewhere on your forums I read that ValueConverters aren't supported within Unity. But that was a few months ago. Has that changed already or is there an estimated release date?
ValueConverters are planned to be implemented for 1.2 version, due by the end of this month approximately.

I hope it helped :)
 
User avatar
Scherub
Topic Author
Posts: 141
Joined: 06 May 2014, 20:53
Contact:

Re: [Unity] Issues with binding controls to a view model

11 May 2014, 18:26

Hi and thanks for your reply.

After comparing your version with mine I figured that you bound it to a float value. I tried to bind it to an integer because I only needed whole numbers. Anyway, this problem is solved.

Okay, my mistake with the ListBox-binding was that I were inadvertently using the generic collection instead of the Noesis non-generic implementation.

The only problem left is that all entries within a combobox appear twice. But I think that's a known problem. Well, that and that you have to use a private setter for the collection property, otherwise it will not bind to it.


Two more questions:

1. Is it possible to group (and sort) items using a CollectionViewSource? I tried this but apparently PropertyGroupDescription is not implemented yet. Is there any way to group items?

2. Is there a way to get some debug output to binding errors, etc.?


Thanks again for your detailed reply. :)

Oh, and good to hear that ValueConverters are on their way! :)


Edit:

Oh, I've nearly forgotten about that one but the ComboBox doesn't recognize the DisplayMemberPath for the selected item.
 
User avatar
sfernandez
Site Admin
Posts: 1915
Joined: 22 Dec 2011, 19:20

Re: [Unity] Issues with binding controls to a view model

12 May 2014, 12:36

After comparing your version with mine I figured that you bound it to a float value. I tried to bind it to an integer because I only needed whole numbers. Anyway, this problem is solved.
Hmmm... binding default converter should be able to convert between integers and floats. I take note to review this, and fix it in case it is necessary.
The only problem left is that all entries within a combobox appear twice. But I think that's a known problem. Well, that and that you have to use a private setter for the collection property, otherwise it will not bind to it.
Yes, this issue was reported recently by another user and you will find it fixed in the next release.
1. Is it possible to group (and sort) items using a CollectionViewSource? I tried this but apparently PropertyGroupDescription is not implemented yet. Is there any way to group items?
Sorry, but grouping and sorting is not implemented yet. If you are interested in this feature you can create a ticket in the bugtracker and we will study it for a future version.
2. Is there a way to get some debug output to binding errors, etc.?
If you place a file named "noesis.ini" in the root of StreamingAssets containing the following lines:
[Gui.Binding]
LogWarnings = true
Warnings about unresolved bindings are output to NoesisGUI.play.log file. This file is generated for an internal tool we use here, but if you open it with a text editor you can perfectly see the messages.
Oh, I've nearly forgotten about that one but the ComboBox doesn't recognize the DisplayMemberPath for the selected item.
You are right, ItemsControl.DisplayMemberPath, as well as Selector.SelectedValue and Selector.SelectedValuePath are not implemented, but we have this features in our roadmap.
 
User avatar
Scherub
Topic Author
Posts: 141
Joined: 06 May 2014, 20:53
Contact:

Re: [Unity] Issues with binding controls to a view model

12 May 2014, 23:54

Grouping and sorting isn't that important. I'm way more interested in a feature such as inline-formatting that were mentioned in another thread. :)

I tried your ini file suggestion and it works quite well. Thanks for that. :)

@other problems: Good to know it's on your roadmap. :)

Who is online

Users browsing this forum: jsantos and 1 guest