将SelectedItem绑定到另一个ListBox中

时间:2013-10-04 11:03:48

标签: xaml mvvm windows-phone bind selecteditem

我正在尝试绑定另一个ListBox中的SelectedItem,但是我从输出中得到了这个绑定错误:

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')..

嵌套列表框声明为:

<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属性:public Dictionary<int, List<ArrivalTime>> TimeTable

时间属性是ArrivalTime

的对象

除了SelectedItem之外,整个绑定工作正常,即使我的VM中有必需的属性

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

感谢您的帮助:)

1 个答案:

答案 0 :(得分:1)

试试这个:

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

如果不能使用RelativeSource,那么就像这样命名你的父容器Grid

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

<ListBox Grid.Column="1" Name="innerList" ItemsSource="{Binding Value}" SelectedItem="{Binding Tag.Time, ElementName=gridFoo}" ScrollViewer.VerticalScrollBarVisibility="Disabled">