locked
Trouble porting Windows Phone 8 app to Windows Store 8.1 - DataTemplate and MVVM issues

    Question

  • I'm having trouble migrating my Windows Phone app to Windows Store. First of all I decided to use Hub control. I started with a blank app and added the Hub sections manually.  I'm not using any templates. Inside each hub section is a DataTemplate.

    This did not exist in my Windows Phone 8 app so now I cannot access my controls by name and more importantly I can't bind to my viewmodel.  I get an error when I try.

    I just want to use the basic ViewModel with old school BindableBase, if that's still possible.  But this DataTemplate is messing everything up?  Can someone offer any advice?


    • Edited by Sal_S Tuesday, April 22, 2014 4:07 AM
    Tuesday, April 22, 2014 4:05 AM

All replies

  • No, you can't access controls by name when they are bound inside a datatemplate.  What you'll have to do is capture a reference in the item's loaded event and store the reference in a list.  It's a bit of a hack but IMO it's the easiest way to get to the controls.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, April 22, 2014 12:58 PM
    Moderator
  • what about the databinding using MVVM?  I'm not able to get it working like I did in Windows Phone

    I have a map control in a hub section, I want to center it on the user position

                <HubSection  Width="900" Background="#FF0E0D23">
                    
                    <HubSection.Header>
                        <TextBlock Text="Location" Margin="0,0,0,0"></TextBlock>
                    </HubSection.Header>
                    
                    <DataTemplate>
                        
                    <Grid >
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"></RowDefinition>
                                <RowDefinition Height="100"></RowDefinition>
                            </Grid.RowDefinitions>
                            <bm:Map  Grid.Row="0" Center="{Binding Path=UserLocation, Mode=OneWay}" Credentials="" x:Name="myMap" Height="500" Width="500" VerticalAlignment="Stretch" ></bm:Map>
                            <Button Grid.Row="1" Width="100" Height="100"></Button>
                        </Grid>
                    </DataTemplate>
               
                </HubSection>

    And in my codebehind and viewModel I'm doing

    public class ViewModel : BindableBase
        {
            private Location _userlocation;
            public Location UserLocation
            {
                get { return _userlocation; }
                set { SetProperty(ref _userlocation, value); }
            }
        }

    I set my data context to the viewmodel

            public MainPage()
            {
                this.InitializeComponent();
                DataContext = App.ViewModel;
                RunTasks();
            }

    But my error is

    Error 1 The text associated with this error code could not be found.

    Failed to assign to property 'Bing.Maps.Map.Center'.  

    Tuesday, April 22, 2014 1:30 PM
  • I'm sorry... MVVM is not a strength of mine.  I have to say that you might need to break the model in order to get the results you desire.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, April 22, 2014 2:11 PM
    Moderator