开发者

Click link on WP7 view model

Im building an app for Windows phone 7. I have a panorama page with a view model - a list of main menu items. I cant work out how to bind these clicks to data, or to create a link to other pages. (Ie; each of this meny items need to be lickable to link to another page).. What I tried is, assigned a click event to the text to {binding Link} in my main viewmodel.cs list of items. Each 'Link' item was a Event handler as per the code shown below in the cs file. This doesnt work so wondering how to achieve this. The example below of the xaml file shows all text links in the list go to the sam开发者_开发百科e Event: 'TextBlock_MouseLeftButtonDown_Wales'. but of course I want them to go to each there own pages.

MainPage.xaml

  <ListBox  Margin="0,0,-12,0" ItemsSource="{Binding Items}" Background="#75000000">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Margin="0,0,0,10">
                            <!--Replace rectangle with image-->
                            <!-- <Rectangle Height="100" Width="100" Fill="#FFE5001b" Margin="12,0,9,0"/> -->
                            <Image Source="/images/image1.png" Height="70" HorizontalAlignment="Left" Margin="0,0,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="77" Opacity="1" OpacityMask="#C8000000" />
                            <StackPanel Width="311">
                                <TextBlock Text="{Binding LineOne}"   MouseLeftButtonDown="TextBlock_MouseLeftButtonDown_Wales"   TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                                <TextBlock Text="{Binding LineTwo}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

MainViewModel.cs

 this.Items.Add(new ItemViewModel() { LineOne = "England", LineTwo = "blahblah", Link = "TextBlock_MouseLeftButtonDown_London" });
        this.Items.Add(new ItemViewModel() { LineOne = "Scotland", LineTwo = "blahblah ", Link = "TextBlock_MouseLeftButtonDown_London" });
        this.Items.Add(new ItemViewModel() { LineOne = "Wales", LineTwo = "blahblah ", Link = "TextBlock_MouseLeftButtonDown_Wales" });
        this.Items.Add(new ItemViewModel() { LineOne = "London", LineTwo = "blablbah ", Link = "TextBlock_MouseLeftButtonDown_London" });

I hope you understand what I am tring to do here, let me know if you need more info. Many thanks. Dan


I'm assuming that your TextBlock_MouseLeftButtonDown_* events are in MainPage.xaml.cs. Is this correct.

If that's the case then, ViewModels aren't designed to work this way.

Have a look at the default code created in a new "DataBound" phone application. That will show an example of using view models which just contain data.

Alternatively you could create an actual method on the ItemViewModel and have the click event on the listitem call the method on the object in the model.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜