如何在XAML flipview中访问命名项目

时间:2012-12-29 15:25:16

标签: windows xaml microsoft-metro winrt-xaml

我正在开发一个应用程序,以在flipview中显示带有一些元数据的照片。这一切都很好,但现在我想将Bing地图添加到RichTextBlocks并根据属于照片的GPS信息显示位置。为了做到这一点,我需要以编程方式从c#代码访问bing Maps控件,对于当前显示的照片集,地图中心控制到照片的位置。从代码隐藏中访问Bing Maps控件(名为myMap)似乎无法做到这一点。

我尝试使用VisualTreeHelper并为treeView获取子项,但是这总是返回null。 代码看起来像:var x = VisualTreeHelper.GetChild(flipView,0); (使用Nuget包for VisualRTXamlToolkit。

这是我的XAML的一部分:

<Grid
        Style="{StaticResource LayoutRootStyle}"
        DataContext="{Binding Group}"      
        d:DataContext="{Binding AllGroups[0], Source={d:DesignInstance Type=data:SampleDataSource, IsDesignTimeCreatable=True}}" >

        <FlipView
            x:Name="flipView"
            AutomationProperties.AutomationId="ItemsFlipView"
            AutomationProperties.Name="Item Details"
            TabIndex="1"
            Padding="0,-3,0,-3"
            Margin="-3,0,3,0"
            ItemsSource="{Binding Source={StaticResource itemsViewSource}}">

            <FlipView.ItemTemplate>
                <DataTemplate>
                    <UserControl Loaded="StartLayoutUpdates" Unloaded="StopLayoutUpdates" x:Name="userControl">
                        <ScrollViewer x:Name="scrollViewer" Style="{StaticResource HorizontalScrollViewerStyle}" Grid.Row="1">

                            <!-- Three-column grid for item-detail layout -->
                            <Grid Margin="0,0,0,0" Height="Auto">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="40" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>

                                <StackPanel Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center">
                                    <ProgressRing x:Name="ProgressRing" Foreground="White" Margin="0" HorizontalAlignment="Center" IsActive="True" MaxHeight="50" MinHeight="50" MaxWidth="50" MinWidth="50"></ProgressRing>
                                    <TextBlock x:Uid="Loading" Text="Loading data" Style="{StaticResource BasicTextStyle}" TextWrapping="Wrap" TextAlignment="Center" Padding="5" HorizontalAlignment="Center" Foreground="White" />
                                </StackPanel>

                                <Image x:Name="image" Width="Auto" Height="Auto" Margin="0,0,0,0" Stretch="Uniform" HorizontalAlignment="Right"
                                       DataContext="{StaticResource itemsViewSource}"
                                       Source="{Binding LargeImage}" Grid.Column="0" />

                                <Grid Margin="0,0,0,20" Height="Auto" Grid.Column="2">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="400" />
                                        <ColumnDefinition Width="Auto" />
                                        <ColumnDefinition Width="Auto" />
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="140" />
                                        <RowDefinition Height="*" />
                                    </Grid.RowDefinitions>

                                    <RichTextBlock Grid.Column="0" Grid.Row="1" Height="Auto" Padding="0,0,20,0" MaxWidth="400" OverflowContentTarget="{Binding ElementName=NextRTB1}" >
                                        <Paragraph>
                                            <Run x:Uid="AltTitle" FontSize="16" FontWeight="Bold" Text="Titel" />
                                        </Paragraph>
                                        <Paragraph Margin="0,0,0,16">
                                            <Run Text="{Binding AltTitle}" FontSize="16" FontWeight="SemiLight" />
                                        </Paragraph>
                                        <Paragraph>
                                            <Run x:Uid="Description" FontSize="16" FontWeight="Bold" Text="Omschrijving" />
                                        </Paragraph>
                                        <Paragraph>
                                            <Run x:Uid="Location" FontSize="16" FontWeight="Bold" Text="Lokatie" />
                                        </Paragraph>
                                        <Paragraph LineStackingStrategy="MaxHeight">
                                            <InlineUIContainer>
                                                <bm:Map Width="400" Height="400" ZoomLevel="6" Credentials="CREDENTIALS HERE" x:Name="myMap">
                                                    <bm:Map.Center>
                                                        <bm:Location Latitude="52" Longitude="5" />
                                                    </bm:Map.Center>
                                                </bm:Map>
                                            </InlineUIContainer>
                                        </Paragraph>
                                        <Paragraph>
                                            <Run x:Uid="CopyRight" FontSize="16" FontWeight="Bold" Text="Bronvermelding" />
                                        </Paragraph>
                                    </RichTextBlock>
                                    <RichTextBlockOverflow x:Name="NextRTB1" OverflowContentTarget="{Binding ElementName=NextRTB2}" Grid.Column="1" Grid.Row="1" Padding="20,0,20,0" MaxWidth="440" />
                                    <RichTextBlockOverflow x:Name="NextRTB2" Grid.Column="2" Grid.Row="1" Padding="20,0,20,0" MaxWidth="440" />
                                </Grid>
                            </Grid>

                            <VisualStateManager.VisualStateGroups>
                                <!-- Visual states reflect the application's view state inside the FlipView -->
                                <VisualStateGroup x:Name="ApplicationViewStates">
                                    <VisualState x:Name="FullScreenLandscape"/>
                                    <VisualState x:Name="Filled"/>

                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                        </ScrollViewer>
                    </UserControl>
                </DataTemplate>
            </FlipView.ItemTemplate>
        </FlipView>

        <!-- Back button -->
        <Button x:Name="backButton" Click="GoBack" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="36,56,0,0"
                    IsEnabled="{Binding Frame.CanGoBack, ElementName=pageRoot}" 
                    Style="{StaticResource BackButtonStyle}" />

    </Grid>

1 个答案:

答案 0 :(得分:0)

无法为FlipView绑定所选项目吗?这可能会更新另一个具有地理编码信息的属性,如下所示:

private PhotoDetail _selectedItem;
public PhotoDetail SelectedItem
{
    get{ return _selectedItem; }
    set
    {
        if (_selectedItem!= value)
        {
            _selectedItem= value;
            if(_selectedItem!=null)
                Center=new GeoCoordinate(_selectedItem.Latitude, _selectedItem.Longitude);

            NotifyPropertyChanged("SelectedItem");
        }
    }

}

private GeoCoordinate _center;
public GeoCoordinate Center
{
    get { return _center; }
    set
    {
        if (_center != value)
        {
            _center = value;
            NotifyPropertyChanged("Center");
        }
    }
}

其中PhotoDetail是flipview中照片的类型。

在地图中,您只需添加绑定:

Center="{Binding Center, Mode=TwoWay}"

在你的flipview中你可以添加

SelectedItem="{Binding SelectedItem, Mode=TwoWay}"