Not all properties being set when assigning to ItemsControl.ItemsSource

Nathan Sokalski 4,126 Reputation points
2020-05-10T19:42:41.003+00:00

I have a UserControl with the following properties:

Public Shared ReadOnly IndexProperty As DependencyProperty = DependencyProperty.Register("Index", GetType(Integer), GetType(BoardSquare), New PropertyMetadata(0))
Public Shared ReadOnly SidesProperty As DependencyProperty = DependencyProperty.Register("Sides", GetType(TravelType()), GetType(BoardSquare), New PropertyMetadata({TravelType.None, TravelType.None, TravelType.None, TravelType.None}, AddressOf SidesIsOverpassChanged))
Public Shared ReadOnly IsOverpassProperty As DependencyProperty = DependencyProperty.Register("IsOverpass", GetType(Boolean), GetType(BoardSquare), New PropertyMetadata(False, AddressOf SidesIsOverpassChanged))

Public Property Index As Integer
 Get
 Return CInt(GetValue(IndexProperty))
 End Get
 Set(ByVal value As Integer)
 SetValue(IndexProperty, value)
 End Set
End Property
Public Property Sides() As TravelType()
 Get
 Return CType(GetValue(SidesProperty), TravelType())
 End Get
 Set(ByVal value As TravelType())
 SetValue(SidesProperty, value)
 End Set
End Property
Public Property IsOverpass() As Boolean
 Get
 Return CBool(GetValue(IsOverpassProperty))
 End Get
 Set(ByVal value As Boolean)
 SetValue(IsOverpassProperty, value)
 End Set
End Property

And in my code I attempt to assign values to ItemsControl.ItemsSource as follows:

Dim originalsquaresides As (Sides As TravelType(), IsOverpass As Boolean)() = {
 ({TravelType.None, TravelType.None, TravelType.None, TravelType.None}, False),
 ({TravelType.Road, TravelType.Road, TravelType.None, TravelType.None}, False),
 ({TravelType.Road, TravelType.Road, TravelType.Road, TravelType.Road}, False),
 ({TravelType.Road, TravelType.Track, TravelType.Road, TravelType.Track}, True),
 ({TravelType.Track, TravelType.None, TravelType.None, TravelType.Road}, False),
 ({TravelType.None, TravelType.None, TravelType.None, TravelType.None}, False)}

Me.itmSquares.ItemsSource = originalsquaresides.Zip(Enumerable.Range(0, originalsquaresides.Length), Function(square, index) New With {.Sides = square.Sides, .IsOverpass = square.IsOverpass, .Index = index})

And my XAML includes the following ItemsControl:

<ItemsControl x:Name="itmSquares" HorizontalAlignment="Center" VerticalAlignment="Center">
 <ItemsControl.ItemsPanel><ItemsPanelTemplate><VariableSizedWrapGrid Orientation="Horizontal" MaximumRowsOrColumns="7"/></ItemsPanelTemplate></ItemsControl.ItemsPanel>
 <ItemsControl.ItemTemplate><DataTemplate><ctrl:BoardSquare Sides="{Binding Sides,Mode=TwoWay}" IsOverpass="{Binding IsOverpass,Mode=TwoWay}" Index="{Binding Index,Mode=TwoWay}" AllowDrop="True" DragEnter="BoardSquare_DragEnter" Drop="BoardSquare_Drop" DragLeave="BoardSquare_DragLeave" SquareChanged="BoardSquare_SquareChanged"/></DataTemplate></ItemsControl.ItemTemplate>
</ItemsControl>

However, when I do this, all of the Sides is set correctly for all elements, but IsOverpass is not set for anything, and Index is only set for the last element. Why is this, and how can I fix it? Thanks.

Universal Windows Platform (UWP)
{count} votes

1 answer

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,316 Reputation points
    2020-05-11T18:09:05.817+00:00

    Hi, I cannot reproduce your problem. I get this result:
    8073-x.png

    XAML in Test-UserControl:

      <Border BorderBrush="Red" BorderThickness="2" Margin="2">  
        <StackPanel x:Name="stp" Margin="5">  
          <TextBlock Text="{Binding Index}"/>  
          <ListBox ItemsSource="{Binding Sides}" Width="100"/>  
          <TextBlock Text="{Binding IsOverpass}"/>  
        </StackPanel>  
      </Border>  
    
    0 comments No comments