将集合绑定到 PivotControl 的 DataTemplate 内的列表选择器

发布于 2024-10-03 09:52:49 字数 3159 浏览 2 评论 0原文

在枢轴控件内,我有一个用于 ItemTemplate 的 DataTemplate (TestItemTemplate)。页面的 DataContext 设置为 {BindingrelativeSource={RelativeSource Self}},并且 PivotControl 的 ItemsSource 绑定到可观察集合。

在数据透视控件的 DataTemplate 内部,我有一个 ListPicker,我想将其绑定到 IEnumerable。我创建了一个公共财产 IEnumerable TestEntries = "一二三".Split();

但列表选择器不显示任何绑定项目。如果我将列表选择器放在数据模板之外(作为 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>

Inside the pivot control, I have a DataTemplate (TestItemTemplate) for the ItemTemplate. The DataContext for the page is set to {Binding RelativeSource={RelativeSource Self}} and the ItemsSource for PivotControl is bound to an observable collection.

Inside the DataTemplate of the pivotcontrol, I have a ListPicker which I want to bind to IEnumerable. I have created a public property of
IEnumerable TestEntries = "One Two Three".Split();

The listpicker doesn't show any bound items though. If I place the listpicker outside the data template (as a sibling of the PivotControl, it shows the three strings in the picker)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

_失温 2024-10-10 09:52:49

我自己想出了这个办法。如果其他人遇到同样的问题,这里是解决方案。
我认为当 ListPicker 位于 DataTemplate 内时,需要正确设置 DataContext,因为在 Page 的初始化方法或加载的事件处理程序中,DataTemplate 内的 ListPicker 仍然为空。使用控件自己的 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>

I figured this out on my own. Here is the solution if others run into the same issue.
I think this is required to set the DataContext properly when the ListPicker is inside a DataTemplate because the in Page's initialize method or the loaded event handler, the ListPicker inside the DataTemplate is still null. Use the control's own Loaded event handler to initialize it.

I had to Set the DataContext of the ListPicker inside its own Loaded eventhandler. Something like this:

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

XAML looks like this:

        <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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文