jc_lvngstn
Topic Author
Posts: 34
Joined: 23 Sep 2013, 03:03

Question about DataBinding

18 Oct 2013, 05:17

Having some issues with databinding, am I missing something obvious?

My listview declaration:
<ListView
            Name="ItemsListView"
            Grid.ColumnSpan="2"
            Grid.Row="1"
            ItemsSource="{Binding Items}">
            <ListView.View>
               <GridView>
                  <GridViewColumn Header="Item">
                     <GridViewColumn.CellTemplate>
                        <DataTemplate>
                           <TextBlock Foreground="White" Text="{Binding Name}"/>
                        </DataTemplate>
                     </GridViewColumn.CellTemplate>
                  </GridViewColumn>
               </GridView>
            </ListView.View>
         </ListView>
I've set the name to ItemsListView, for retrieval via code. The ItemSource is bound to Items.
// Use this for initialization
	private void Start()
	{
		_uiPanel = GetComponent<NoesisGUIPanel>();
		_Root = _uiPanel.GetRoot<FrameworkElement>();
		_DrawingCanvas = _Root.FindName<Canvas>("MainWindow");
		
		// Tried putting this at the end of Start(), didn't make a difference.
		_DrawingCanvas.SetDataContext(_InventoryData);

		_InventoryData = new MyInventoryData { Items = new Collection() }; // Set data context for entire panel to this.

		MyInventoryItemData item = new MyInventoryItemData { Name = "Wooden Sword", Quality = 75.3f, Weight = 12.5f }; // Add an item to the inventory
		_InventoryData.Items.Add(item);


	}
}

[Extended]
public class MyInventoryData : BaseComponent
{
	public Collection Items;
	public string Title;
}

[Extended]
public class MyInventoryItemData : BaseComponent
{
	public string Name;
	public float Quality;
	public float Weight;
}
I've also gotten the name of the listview, and tried setting ItemsSource...I got a blank entry in the listview, but nothing else.
Is there something I'm just missing?

Thanks,
JC
 
User avatar
sfernandez
Site Admin
Posts: 3222
Joined: 22 Dec 2011, 19:20

Re: Question about DataBinding

18 Oct 2013, 09:54

The first thing I noticed is that you are probably setting the DataContext property to a null value, because you are creating the inventory data instance later. It should be done the opposite:
_InventoryData = new MyInventoryData { Items = new Collection() };
_DrawingCanvas.SetDataContext(_InventoryData);
In databinding, if you want that UI reflects the changes of your data items, they should notify when a property changes. The code for your data context and items should look like this:
[Extended]
public class MyInventoryData : BaseComponent
{
    private Collection _items;
    public Collection Items
    {
        get
        {
            return _items;
        }
        set
        {
            if (_items != value)
            {
                _items = value;
                NotifyPropertyChanged("Items");
            }
        }
    }

    private string _title;
    public string Title
    {
        get
        {
            return _title;
        }
        set
        {
            if (_title != value)
            {
                _title = value;
                NotifyPropertyChanged("Title");
            }
        }
    }
}

[Extended]
public class MyInventoryItemData : BaseComponent
{
    private string _name;
    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            if (_name != value)
            {
                _name = value;
                NotifyPropertyChanged("Name");
            }
        }
    }

    private float _quality;
    public float Quality
    {
        get
        {
            return _quality;
        }
        set
        {
            if (_quality != value)
            {
                _quality = value;
                NotifyPropertyChanged("Quality");
            }
        }
    }

    private float _weight;
    public float Weight
    {
        get
        {
            return _weight;
        }
        set
        {
            if (_weight != value)
            {
                _weight = value;
                NotifyPropertyChanged("Weight");
            }
        }
    }
}
After these changes I was able to see the item "Wooden Sword" in the ListView ;)
 
jc_lvngstn
Topic Author
Posts: 34
Joined: 23 Sep 2013, 03:03

Re: Question about DataBinding

19 Oct 2013, 01:16

Thanks for your reply!
That example probably wasn't very accurate, I did actually notice the null assignment. I had been changing the code around a bit, and that's what got pasted, sorry :)

The confusion with the notification was because...from what I recall, with MS WPF, you normally don't have to notify when you initially bind to collections or properties with values to have them show up initially. Yes, after initialization if I modify the values and expect them to update the UI I have to notify of property changed, but I don't believe this is required at initialization with Microsoft VS/WPF. Am I crazy, or does Noesis work a little different?

Thanks though, what you posted works. I appreciate you taking the time to point me in the right direction!
 
User avatar
sfernandez
Site Admin
Posts: 3222
Joined: 22 Dec 2011, 19:20

Re: Question about DataBinding

21 Oct 2013, 14:26

If you only want to show the initial value, you should at least define your data members as properties. In WPF bindings doesn't work for fields.

Your code should look like this then:
[Extended]
public class MyInventoryData : BaseComponent
{
    public Collection Items { get; set; }
    public string Title { get; set; }
}

[Extended]
public class MyInventoryItemData : BaseComponent
{
    public string Name { get; set; }
    public float Quality { get; set; }
    public float Weight { get; set; }
}
 

Who is online

Users browsing this forum: Semrush [Bot] and 2 guests