【问题标题】:Click link on WP7 view model单击 WP7 视图模型上的链接
【发布时间】:2010-12-08 17:29:08
【问题描述】:

我正在为 Windows phone 7 构建一个应用程序。我有一个带有视图模型的全景页面 - 一个主菜单项列表。我不知道如何将这些点击绑定到数据,或创建指向其他页面的链接。 (即;每个项目都需要可被链接到另一个页面)。我尝试的是,在我的主 viewmodel.cs 项目列表中为文本分配一个点击事件以 {binding Link} 。根据下面 cs 文件中显示的代码,每个“链接”项都是一个事件处理程序。这不起作用,所以想知道如何实现这一点。 下面的 xaml 文件示例显示列表中的所有文本链接都转到同一事件:“TextBlock_MouseLeftButtonDown_Wales”。但我当然希望他们去每个那里都有自己的页面。

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" });

我希望你明白我在这里做什么,如果你需要更多信息,请告诉我。非常感谢。丹

【问题讨论】:

    标签: c# windows silverlight mvvm windows-phone-7


    【解决方案1】:

    我假设您的TextBlock_MouseLeftButtonDown_* 事件在MainPage.xaml.cs 中。它是否正确。

    如果是这种情况,那么 ViewModel 就不是为这种方式设计的。

    查看在新的“DataBound”电话应用程序中创建的默认代码。这将展示一个使用仅包含数据的视图模型的示例。

    或者,您可以在ItemViewModel 上创建一个实际方法,并让列表项上的点击事件调用模型中对象上的方法。

    【讨论】:

    • 谢谢马特,我会按照你的思路做更多的研究。是的,你是正确的 TextBlock_MouseLeftButtonDown_* 在 mainpage.xaml.cs 中。
    • @Dan 你最后去了哪条路?
    猜你喜欢
    • 2018-02-17
    • 2016-03-02
    • 1970-01-01
    • 1970-01-01
    • 2015-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多