njsokalski avatar image
njsokalski asked ·

Not all properties being set when assigning to ItemsControl.ItemsSource

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
  Return CInt(GetValue(IndexProperty))
  End Get
  Set(ByVal value As Integer)
  SetValue(IndexProperty, value)
  End Set
 End Property
 Public Property Sides() As TravelType()
  Return CType(GetValue(SidesProperty), TravelType())
  End Get
  Set(ByVal value As TravelType())
  SetValue(SidesProperty, value)
  End Set
 End Property
 Public Property IsOverpass() As Boolean
  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>

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.

10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

What does "but IsOverpass is not set for anything" mean? When I tested the IsOverpass property, its value is equal to what you set, what's your unexpected behavior? In addition, in the zip method, the value of index property returns each element of originalsquaresides, do you want to get the index of each element?

0 Votes 0 · ·

Hi, have you solved your issue? Do you have other questions?

0 Votes 0 · ·

Hi, is there any update?

0 Votes 0 · ·

1 Answer

PeterFleischer-3316 avatar image
PeterFleischer-3316 answered ·

Hi, I cannot reproduce your problem. I get this result:

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}"/>

x.png (34.0 KiB)
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.