数据模板中的XAML绑定项目源

时间:2016-08-15 12:47:41

标签: xaml mvvm

我试图将位于我的列表框元素中的可观察集合(关键帧)绑定到我的datatemplate的itemssource(可观察集合)

<ListBox x:Name="lbTimeLines"  
DataContext="{Binding MainViewport,Source={StaticResource Locator}}" 
ItemsSource="{Binding AnimateableObjects}"  SelectedIndex="{Binding selectedIndex}">
        <ListBox.ItemTemplate >
                        <DataTemplate DataType="{x:Type obj:ObjectSettings }">
                            <cc:TimeLine x:Name="TL" Height="25"  
                                         ItemsSource="{Binding Path=KeyFrames}"<!-- here is the problem -->
                                         CurrentFrame="{Binding LayerView.CurrentFrame,Source={StaticResource Locator},Mode=TwoWay}"   
                                         Width="{Binding LayerView.Globalwidth,Source={StaticResource Locator}}">
                            </cc:TimeLine>
                        </DataTemplate>
       </ListBox.ItemTemplate>

然而,这似乎导致错误

  

System.Windows.Data错误:40:BindingExpression路径错误:'对象'''TimeLineViewModel'(HashCode = 2312607)'上找不到'KeyFrames'属性。 BindingExpression:路径=关键帧; DataItem ='TimeLineViewModel'(HashCode = 2312607); target元素是'TimeLine'(Name ='MainControl'); target属性是'ItemsSource'(类型'ObservableCollection`1')

不是为什么,但他似乎在TimeLine的viewmodel而不是listbox元素中寻找KeyFrames

注意:我在不同的列表框上使用类似的绑定,但这似乎工作正常

<ListBox  x:Name="lbLayers"  
DataContext="{Binding MainViewport,Source={StaticResource Locator}}" 
ItemsSource="{Binding AnimateableObjects}" SelectedIndex="{Binding selectedIndex}">
<ListBox.ItemTemplate>
    <DataTemplate DataType="{x:Type obj:ObjectSettings }">
        <StackPanel >
            <Label Content="{Binding Name}" Height="25" Width="180"></Label>
        </StackPanel>
    </DataTemplate>
</ListBox.ItemTemplate>

1 个答案:

答案 0 :(得分:0)

当我在等待答案时,我已经更改了我的xaml,因此关键帧将被绑定到隐藏的itemscontrol,并且我的时间轴的itemssource被绑定到itemscontrol itemssource

这让我继续工作

<ListBox x:Name="lbTimeLines"  DataContext="{Binding MainViewport,Source={StaticResource Locator}}" ItemsSource="{Binding AnimateableObjects}"  SelectedIndex="{Binding selectedIndex}">
<ListBox.ItemTemplate >
    <DataTemplate DataType="{x:Type obj:ObjectSettings }" >
        <StackPanel Orientation="Horizontal">
            <ItemsControl x:Name="Testcontainer" ItemsSource="{Binding KeyFrames, Mode=TwoWay}" Visibility="Hidden" Width="0">
            </ItemsControl>
            <cc:TimeLine Height="25"  
                     ItemsSource="{Binding ElementName=Testcontainer, Path=ItemsSource,Mode=TwoWay}"
                     CurrentFrame="{Binding LayerView.CurrentFrame,Source={StaticResource Locator},Mode=TwoWay}"   
                     Width="{Binding LayerView.Globalwidth,Source={StaticResource Locator}}">
            </cc:TimeLine>
        </StackPanel>
    </DataTemplate>
</ListBox.ItemTemplate>

在第一个回答后我将我的代码更改回原始版本,但我无法重现错误