ItemsControl Bindings
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:
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:
Any directions are apreciated!
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:
Code: Select all
<!-- 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>
However, my Binding seems to fail:
Code: Select all
[NOESIS] Binding failed: Path=Content.ItemName, Source=BindingExpression(''), Target=Border(''), TargetProperty=ToolTipService.ToolTip
-
sfernandez
Site Admin
- Posts: 3154
- Joined:
Re: ItemsControl Bindings
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?
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?
Re: ItemsControl Bindings
Hey there, thank you for taking your time!
Yeah, the properties should match with what I've bound.
MainViewModel:
HotBar:
Slot:
I've overlooked the warning message you are talking about:
So it seems, like the DataContext is wrong for generated controls?
Yeah, the properties should match with what I've bound.
MainViewModel:
Code: Select all
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
}
Code: Select all
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;
}
}
Code: Select all
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;
}
}
Code: Select all
Cant't solve PropertyPath: Type 'BindingExpression' does not contain a property 'Content'
-
sfernandez
Site Admin
- Posts: 3154
- Joined:
Re: ItemsControl Bindings
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?
Could you please report this issue in our bugtracker?
-
sfernandez
Site Admin
- Posts: 3154
- Joined:
Re: ItemsControl Bindings
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:
Could you please try to change in the ItemsControl, instead of binding to the Items property, try to bind to the ItemsSource property:
Code: Select all
<ItemsControl ItemsSource="{Binding HotBar.Slots}">
Re: ItemsControl Bindings
Hey, yeah, that was it. Couldn't see the forest for the trees.
-
sfernandez
Site Admin
- Posts: 3154
- Joined:
Re: ItemsControl Bindings
Great, marking this as solved then.
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 3 guests