将集合绑定到 PivotControl 的 DataTemplate 内的列表选择器
在枢轴控件内,我有一个用于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我自己想出了这个办法。如果其他人遇到同样的问题,这里是解决方案。
我认为当 ListPicker 位于 DataTemplate 内时,需要正确设置 DataContext,因为在 Page 的初始化方法或加载的事件处理程序中,DataTemplate 内的 ListPicker 仍然为空。使用控件自己的 Loaded 事件处理程序对其进行初始化。
我必须在其自己的 Loaded 事件处理程序中设置 ListPicker 的 DataContext。像这样:
XAML 看起来像这样:
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:
XAML looks like this: