Question

I'm trying to bind SelectedItem, which is inside another ListBox, however I get this binding error from output:

System.Windows.Data Error: BindingExpression path error: 'Time' property not found on '[23, System.Collections.Generic.List`1[MarsrutaiAPI.ArrivalTime]]' 'System.Collections.Generic.KeyValuePair`2[System.Int32,System.Collections.Generic.List`1[MarsrutaiAPI.ArrivalTime]]' (HashCode=1268928309). BindingExpression: Path='Time' DataItem='[23, System.Collections.Generic.List`1[MarsrutaiAPI.ArrivalTime]]' (HashCode=1268928309); target element is 'System.Windows.Controls.ListBox' (Name='innerList'); target property is 'SelectedItem' (type 'System.Object')..

The nested listbox is declared as such:

<ListBox ItemsSource="{Binding TimeTable}" Visibility="{Binding IsLoading, Converter={StaticResource BooleanToVisibilityNegativeConverter}}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="70"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <TextBlock Text="{Binding Key}" Style="{StaticResource NormalText}" FontSize="42" Margin="0,0,10,0" HorizontalAlignment="Center" />
                        <ListBox Grid.Column="1" Name="innerList" ItemsSource="{Binding Value}" SelectedItem="{Binding Time,Mode=TwoWay}" ScrollViewer.VerticalScrollBarVisibility="Disabled">
                            <ListBoxItem Margin="0,0,5,5"/>
                            <ListBox.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <toolkit:WrapPanel Orientation="Horizontal"/>
                                </ItemsPanelTemplate>
                            </ListBox.ItemsPanel>
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <Border BorderBrush="{StaticResource tekstas}" BorderThickness="1" CornerRadius="5" Margin="5" Background="{Binding IsAccessible,Converter={StaticResource IsAccessibleToColor}}">
                                        <TextBlock Style="{StaticResource NormalText}" Text="{Binding ExpectedTime, StringFormat=\{0:mm\}}" Margin="10" HorizontalAlignment="Center"/>
                                    </Border>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                        <Border Grid.Row="1" Grid.ColumnSpan="2" Margin="10"/>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

TimeTable property:public Dictionary<int, List<ArrivalTime>> TimeTable

Time property is an object of ArrivalTime

The whole binding works perfectly, except for the SelectedItem, even though I have the required property in my VM

private ArrivalTime _time;
    public ArrivalTime Time
    {
        get { return _time; }
        set
        {
            _time = value;
            RaisePropertyChanged("Time");
        }
    }

Thanks for the help :)

Was it helpful?

Solution

try this:

SelectedItem="{Binding Path=Time, RelativeSource={RelativeSource AncestorType={x:Type Grid}}}"

And if RelativeSource is not possible then name your Parent container Grid like this

<Grid Name="gridFoo" Tag={Binding} />

And

<ListBox Grid.Column="1" Name="innerList" ItemsSource="{Binding Value}" SelectedItem="{Binding Tag.Time, ElementName=gridFoo}" ScrollViewer.VerticalScrollBarVisibility="Disabled">
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top