ItemContainerStyle中的数据绑定 - WPF - MVVM Light

时间:2013-03-31 00:50:38

标签: wpf data-binding custom-controls mvvm-light itemcontainerstyle

我正在创建一个带有使用Grid的ItemContainer的自定义ListBox。目标是将每个项目添加到特定列,行,并具有特定的行跨。这些数字在ObservableCollection ScheduledCourses中设置为可绑定属性。在DataTemplate上,我能够指定DataType,然后能够从集合中进行绑定。但我无法在Style上指定DataType。如何在BlockPlacement中设置值以绑定所需的属性?

PS。我正在使用MVVM Light。

<UserControl.Resources>
    <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
    <CollectionViewSource x:Key="CoursesByDay" Source="{Binding ScheduledCourses}">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="MeetsOn" />
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>

    <DataTemplate x:Key="ScheduleBlock" DataType="vm:ScheduleBlockViewModel">
        <userControls:ScheduleBlock />
    </DataTemplate>

    <ItemsPanelTemplate x:Key="ScheduleDayGrid">
        <!--  Replace with Schedule Day Grid  -->
        <Grid Name="ScheduleDayGrid"
              Width="400"
              Initialized="ScheduleDayGridInitialized"
              ShowGridLines="True" />
    </ItemsPanelTemplate>

    <Style x:Key="BlockPlacement" TargetType="ListBoxItem">
        <Setter Property="Grid.Column" Value="{Binding Path=ScheduledCourses[].TimeSlotIndex}" />
        <Setter Property="Grid.Row" Value="{Binding ScheduledCourses[].StartTimeIndex}" />
        <Setter Property="Grid.RowSpan" Value="{Binding ScheduledCourses[].DurationIndex}" />
        <Setter Property="VerticalContentAlignment" Value="Top" />
        <Setter Property="Grid.VerticalAlignment" Value="Stretch" />
    </Style>
</UserControl.Resources>
<UserControl.DataContext>
    <Binding Mode="OneWay"
             Path="ScheduleGrid"
             Source="{StaticResource Locator}" />
</UserControl.DataContext>

0 个答案:

没有答案