arvenyon
Topic Author
Posts: 4
Joined: 10 Sep 2024, 12:52

ItemsControl Bindings

27 Oct 2024, 12:55

Good day,

I'm sure this is a very trivial question, but I couldn't find a similar question here on the forum and the documentation for ItemControl is a bit sparse.
I'm simply trying to create a quickly put together hotbar. I opted for an ItemsControl with a horizontally oriented stackpanel as ItemsPanel.

Here's The XAML:

 <!-- DataContext for UserControl is set in code behind -->

  <Grid Margin="10">
	  <Grid.RowDefinitions>
		  <RowDefinition Height="Auto" />
		  <RowDefinition Height="*" />
		  <RowDefinition Height="Auto" />
	  </Grid.RowDefinitions>

	  <Grid Grid.Row="2"
			Margin="0 10 0 0">
		  <Grid.ColumnDefinitions>
			  <ColumnDefinition Width="*" />
			  <ColumnDefinition Width="Auto" />
			  <ColumnDefinition Width="*" />
		  </Grid.ColumnDefinitions>

		  <Button Content="DBG"
				  Height="50"
				  Width="50" 
				  Command="{Binding DebugCommand}"/>

		  <Border CornerRadius="8"
				  BorderThickness="1"
				  BorderBrush="Silver"
				  Background="DimGray"
				  Grid.Column="1"
				  Padding="0 5">
				  
			  <ItemsControl Items="{Binding HotBar.Slots}">
				  <ItemsControl.ItemsPanel>
					  <ItemsPanelTemplate>
						  <StackPanel Orientation="Horizontal" />
					  </ItemsPanelTemplate>
				  </ItemsControl.ItemsPanel>
				  <ItemsControl.ItemTemplate>
					  <DataTemplate DataType="{x:Type Slot}">
						  <Border Height="50"
								  Width="50"
								  Margin="5 0"
								  Background="Gray"
								  BorderThickness="1"
								  BorderBrush="Silver"
								  ToolTip="{Binding Content.ItemName}"
								  CornerRadius="5" />
					  </DataTemplate>
				  </ItemsControl.ItemTemplate>
			  </ItemsControl>
		  </Border>
	  </Grid>
</Grid>
I'm a WPF developer, and the expected behaviour would be, that the generated controls have their DataContext automatically set to the current item during the iteration.
However, my Binding seems to fail:
[NOESIS] Binding failed: Path=Content.ItemName, Source=BindingExpression(''), Target=Border(''), TargetProperty=ToolTipService.ToolTip
Any directions are apreciated!
 
User avatar
sfernandez
Site Admin
Posts: 3154
Joined: 22 Dec 2011, 19:20

Re: ItemsControl Bindings

28 Oct 2024, 11:43

Hi,

I guess the HotBar.Slots is a collection of Slot items, does that Slot view model expose a property named "Content"?
Before that Binding failed message there should be another one with more info explaining why it failed, what does it say?
 
arvenyon
Topic Author
Posts: 4
Joined: 10 Sep 2024, 12:52

Re: ItemsControl Bindings

28 Oct 2024, 21:57

Hey there, thank you for taking your time!

Yeah, the properties should match with what I've bound.

MainViewModel:
public class MainViewModel : NotifyObject
{
	#region Commands
	public ICommand DebugCommand => new RelayCommand(
		_ =>
		{
			var pickaxeItem = Database.Get().Get<PickaxeItem>("DEBUG_PICKAXE");
			var itemStack = new ItemStack(pickaxeItem, 25);

			HotBar.Set(0, itemStack);
		});
	#endregion

	#region Properties
	public HotBar HotBar { get; } = new();
	#endregion
}
HotBar:
public class HotBar : NotifyObject
{
	public ReadOnlyObservableCollection<Slot> Slots { get; }

	private readonly ObservableCollection<Slot> m_Slots = new();

    public HotBar()
    {
		Slots = new(m_Slots);

		for (int i = 0; i < 10; i++)
		{
			m_Slots.Add(new(i));
		}
    }

    public void Set(int index, ItemStack stack)
	{
		Slots.ElementAt(index).Content = stack;
	}

	public void Clear(int index)
	{
		Slots.ElementAt(index).Content = null;
	}
}
Slot:
public class Slot : NotifyObject
{
	public int Index => m_Index;
	public ItemStack Content
	{
		get => GetValue<ItemStack>();
		set => SetValue(value);
	}

	private readonly int m_Index;

    public Slot(int index)
    {
		m_Index = index;
    }
}
I've overlooked the warning message you are talking about:
Cant't solve PropertyPath: Type 'BindingExpression' does not contain a property 'Content'
So it seems, like the DataContext is wrong for generated controls?
 
User avatar
sfernandez
Site Admin
Posts: 3154
Joined: 22 Dec 2011, 19:20

Re: ItemsControl Bindings

29 Oct 2024, 17:56

I need to reproduce it, because it makes no sense that the source of the binding is a BindingExpression object, it should be the item (Slot) of the collection.
Could you please report this issue in our bugtracker?
 
User avatar
sfernandez
Site Admin
Posts: 3154
Joined: 22 Dec 2011, 19:20

Re: ItemsControl Bindings

29 Oct 2024, 18:00

I've just saw something that could be the source of the problem.

Could you please try to change in the ItemsControl, instead of binding to the Items property, try to bind to the ItemsSource property:
<ItemsControl ItemsSource="{Binding HotBar.Slots}">
 
arvenyon
Topic Author
Posts: 4
Joined: 10 Sep 2024, 12:52

Re: ItemsControl Bindings

01 Nov 2024, 14:00

Hey, yeah, that was it. Couldn't see the forest for the trees.
 
User avatar
sfernandez
Site Admin
Posts: 3154
Joined: 22 Dec 2011, 19:20

Re: ItemsControl Bindings

05 Nov 2024, 12:18

Great, marking this as solved then.

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 3 guests