2012-12-16 200 views
0

我想使用下面的代碼將數據綁定到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

回答

1

原來這很簡單:

<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>