WPF嵌套数据绑定

问题描述:

我想使用下面的代码将数据绑定到WPF中嵌套的StackPanels - 无济于事。任何人都可以发现我要出错的地方吗?WPF嵌套数据绑定

<Page.Resources> 
    <DataTemplate x:Key="MinuteTimeSlotTemplate"> 
     <TextBlock Text="{Binding Path=TourName}" Background="Blue" /> 
    </DataTemplate> 
    <DataTemplate x:Key="HourlyTimeSlotTemplate"> 
     <StackPanel> 
      <Border Height="120" BorderThickness="0,0,0,1" BorderBrush="#DDD" x:Name="parentRow"> 
       <Grid> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="123"/> 
         <ColumnDefinition/> 
        </Grid.ColumnDefinitions> 
        <StackPanel Orientation="Vertical" VerticalAlignment="Center" Margin="0,0,10,0" MouseUp="StackPanel_MouseUp_1"> 
         <TextBlock Foreground="#4585B9" FontWeight="Bold" FontFamily="Trebuchet MS" FontSize="20" HorizontalAlignment="Right"> 
          <Run Text="{Binding Path=Time}"/></TextBlock> 
         <TextBlock FontFamily="Trebuchet MS" Foreground="#808080" HorizontalAlignment="Right"><Run Text="View Bookings"/></TextBlock> 
        </StackPanel> 
        <StackPanel Orientation="Vertical" Grid.Column="1" Margin="10,0,10,0" x:Name="stk"> 
         <ItemsControl ItemTemplate="{StaticResource MinuteTimeSlotTemplate}" ItemsSource="{Binding Path=HourlySlots}" > 
          <ItemsControl.ItemsPanel> 
           <ItemsPanelTemplate> 
            <VirtualizingStackPanel Orientation="Vertical"/> 
           </ItemsPanelTemplate> 
          </ItemsControl.ItemsPanel> 
         </ItemsControl> 
        </StackPanel> 
       </Grid> 
      </Border> 
     </StackPanel> 
    </DataTemplate> 
</Page.Resources> 
<helpers:AnimatedScrollViewer x:Name="scrlHours" Grid.Column="1" PanningMode="Both" PanningDeceleration="5" PanningRatio="2"> 
    <ItemsControl x:Name="TimeSlotList" ItemTemplate="{StaticResource HourlyTimeSlotTemplate}" > 
     <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate> 
       <VirtualizingStackPanel Orientation="Vertical"/> 
      </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
    </ItemsControl> 
</helpers:AnimatedScrollViewer> 

我设置父StackPanels的ItemsSource代码:

public HourlyCalendar() { 
     InitializeComponent(); 
     TimeSlotList.ItemsSource = new Models.TimeSlots(DateTime.Today).HourlySlots; 
    } 

这是我的模型:

public class TimeSlots { 

    public TimeSlots(DateTime? day) { 
     this.HourlySlots = DataManager.GetCalendarDayTimeSlots(day); 
    } 

    public IEnumerable<TimeSlot> HourlySlots { get; set; } 

} 

父StackPanel的结合符合市场预期,但我无法弄清楚如何绑定孩子StackPanel ...

+0

您是否尝试过绑定的Text?我没有使用,但我使用了Text属性。 – kenny

+0

干杯肯尼。我设法解决这个问题,将标记为“2天内”的答案! – Leigh

原来这很简单:

<helpers:AnimatedScrollViewer x:Name="scrlHours" Grid.Column="1" PanningMode="Both" PanningDeceleration="5" PanningRatio="2"> 
    <ItemsControl x:Name="TimeSlots"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <Border Height="120" BorderThickness="0,0,0,1" BorderBrush="#DDD" x:Name="parentRow"> 
        <StackPanel Orientation="Horizontal"> 
         <StackPanel Orientation="Vertical" VerticalAlignment="Center" Margin="0,0,10,0" Width="123" MouseUp="StackPanel_MouseUp_1"> <!--Height="{Binding Height, ElementName=parentRow}"--> 
          <TextBlock Foreground="#4585B9" FontWeight="Bold" FontFamily="Trebuchet MS" FontSize="20" HorizontalAlignment="Right"> 
          <Run Text="{Binding Path=Time}"/></TextBlock> 
          <TextBlock FontFamily="Trebuchet MS" Foreground="#808080" HorizontalAlignment="Right"><Run Text="View Bookings"/></TextBlock> 
         </StackPanel> 
         <ListView ItemsSource="{Binding Bookings}" ScrollViewer.CanContentScroll="False"> 
          <ListBox.ItemTemplate> 
           <DataTemplate> 
            <TextBlock Text="{Binding TourName}"></TextBlock> 
           </DataTemplate> 
          </ListBox.ItemTemplate> 
         </ListView> 
        </StackPanel> 
       </Border> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 
</helpers:AnimatedScrollViewer>