将集合绑定到PivotControl的DataTemplate中的listpicker

时间:2010-11-18 22:35:48

标签: wpf datatemplate

在pivot控件中,我有一个ItemTemplate的DataTemplate(TestItemTemplate)。页面的DataContext设置为{Binding RelativeSource = {RelativeSource Self}},PivotControl的ItemsSource绑定到可观察的集合。

在pivotcontrol的DataTemplate中,我有一个ListPicker,我想绑定到IEnumerable。我创建了一个公共财产 IEnumerable TestEntries =“一二三”.Split();

listpicker虽然没有显示任何绑定的项目。如果我将listpicker放在数据模板之外(作为PivotControl的兄弟,它会在选择器中显示三个字符串)

    <phone:PhoneApplicationPage.Resources>
    <DataTemplate x:Key="TestItemTemplate">
     <Grid Margin="0,-25,0,0">         
      <Grid.RowDefinitions>
          <RowDefinition Height="Auto" x:Name="AnotherContainer" />
          <RowDefinition Height="300" x:Name="TestDescriptionContainer" />
          <RowDefinition Height="Auto" x:Name="SaveCancelDeleteContainer" />            
      </Grid.RowDefinitions>

            <toolkit:ListPicker x:Name="lstPicker" Grid.Row="0" ItemsSource="{Binding TestEntries}" Header="situation" FullModeHeader="SITUATIONS">
                <toolkit:ListPicker.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding}" Margin="4 0 0 0"/>
                        </StackPanel>
                    </DataTemplate>
                </toolkit:ListPicker.ItemTemplate>
                <toolkit:ListPicker.FullModeItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Margin="16 21 0 20">
                            <TextBlock Text="{Binding}" Margin="4 0 0 0" FontSize="43" FontFamily="{StaticResource PhoneFontFamilyLight}"/>
                        </StackPanel>
                    </DataTemplate>
                </toolkit:ListPicker.FullModeItemTemplate>
            </toolkit:ListPicker>   

<TextBox Grid.Row="1" Text="{Binding Description}" TextWrapping="Wrap" VerticalAlignment="Top" d:LayoutOverrides="Width" AcceptsReturn="True" Height="300"/>   

      <StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,2,0,0" >
          <Button x:Name="SaveButton" Content="Save" Margin="5" Click="SaveButton_Click" Width="140" />
          <Button x:Name="CancelButton" Content="Cancel" Margin="5" Click="CancelButton_Click" Width="140" />
          <Button x:Name="DeleteButton" Content="Delete" Margin="5" Click="DeleteButton_Click" Width="140" />
      </StackPanel>    
     </Grid>
    </DataTemplate>   
</phone:PhoneApplicationPage.Resources>

<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
    <!--Pivot Control-->

    <controls:Pivot 
        x:Name="PivotControl"
        Title="{StaticResource AppName}" 
        ItemsSource="{Binding TestEntries}" 
        ItemTemplate="{StaticResource TestItemTemplate}"
        SelectionChanged="PivotControl_SelectionChanged"
        >
    </controls:Pivot>
</Grid>

1 个答案:

答案 0 :(得分:1)

我自己想出来了。如果其他人遇到同样的问题,这是解决方案。 我认为当ListPicker在DataTemplate中时需要正确设置DataContext,因为在Page的initialize方法或加载的事件处理程序中,DataTemplate中的ListPicker仍为null。使用控件自己的Loaded事件处理程序对其进行初始化。

我必须在自己的Loaded事件处理程序中设置ListPicker的DataContext。像这样:

private void lstTestEntriesPicker_Loaded(object sender, RoutedEventArgs e)
{
    ListPicker lstTestEntriesPicker= VisualElementHelper.FindName<ListPicker>("lstTestEntriesPicker", this);
    if (lstTestEntriesPicker!= null)
    {
        lstTestEntriesPicker.DataContext = TestEntries;
    }
}

XAML看起来像这样:

        <toolkit:ListPicker x:Name="lstTestEntriesPicker"  ItemsSource="{Binding}" Grid.Row="0" Header="TestEntries" FullModeHeader="TestEntries" Loaded="lstTestEntriesPicker_Loaded">
            <toolkit:ListPicker.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding}" Margin="4 0 0 0"/>
                    </StackPanel>
                </DataTemplate>
            </toolkit:ListPicker.ItemTemplate>
            <toolkit:ListPicker.FullModeItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" Margin="16 21 0 20">
                        <TextBlock Text="{Binding}" Margin="4 0 0 0" FontSize="43" FontFamily="{StaticResource PhoneFontFamilyLight}"/>
                    </StackPanel>
                </DataTemplate>
            </toolkit:ListPicker.FullModeItemTemplate>
        </toolkit:ListPicker>