-
- jc_lvngstn
- Posts: 34
- Joined:
Question about DataBinding
Having some issues with databinding, am I missing something obvious?
My listview declaration:
I've set the name to ItemsListView, for retrieval via code. The ItemSource is bound to Items.
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
My listview declaration:
Code: Select all
<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>
Code: Select all
// 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;
}
Is there something I'm just missing?
Thanks,
JC
-
-
sfernandez
Site Admin
- Posts: 3222
- Joined:
Re: Question about DataBinding
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:
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:
After these changes I was able to see the item "Wooden Sword" in the ListView 
Code: Select all
_InventoryData = new MyInventoryData { Items = new Collection() };
_DrawingCanvas.SetDataContext(_InventoryData);
Code: Select all
[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");
}
}
}
}

-
- jc_lvngstn
- Posts: 34
- Joined:
Re: Question about DataBinding
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!
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!
-
-
sfernandez
Site Admin
- Posts: 3222
- Joined:
Re: Question about DataBinding
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:
Your code should look like this then:
Code: Select all
[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