Windows phone 7 如何在listbox的textblock上添加click事件?

Windows phone 7 如何在listbox的textblock上添加click事件?,windows-phone-7,xaml,listbox,onclick,textblock,Windows Phone 7,Xaml,Listbox,Onclick,Textblock,我正在开发WindowsPhone7应用程序。我的应用程序中有一个列表框。我正在用这个列表框进行动态绑定。列表框的代码如下: <ListBox x:Name="FirstListBox" Margin="0,0,-12,0" ItemsSource="{Binding Items}" > <ListBox.ItemTemplate> <DataTemplate> <StackPanel Margin="0,

我正在开发WindowsPhone7应用程序。我的应用程序中有一个列表框。我正在用这个列表框进行动态绑定。列表框的代码如下:

<ListBox x:Name="FirstListBox" Margin="0,0,-12,0" ItemsSource="{Binding Items}" >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Margin="0,0,0,17" Width="432">
                <TextBlock  Text="{Binding ID}" Width="440"></TextBlock>
                <TextBlock Text="{Binding IsCompany}" Width="440"></TextBlock>                                                                       
                <TextBlock Foreground="Red" Text="{Binding CompanyORIndividual}" Width="440"></TextBlock>                                                                            
                <TextBlock  Text="{Binding MicrosoftContact}" Width="440"></TextBlock>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
    <ListBox.Template>
        <ControlTemplate>
            <ScrollViewer HorizontalScrollBarVisibility="Visible">
                <ItemsPresenter />
            </ScrollViewer>
        </ControlTemplate>
        </ListBox.Template>
</ListBox>

此列表框位于pivot控件内。我想在下面的文本块中添加链接或单击事件一

<TextBlock Foreground="Red" Text="{Binding CompanyORIndividual}" Width="440"></TextBlock>


因此,在单击CompanyORIndividual textblock之后,用户应该被导航到另一个xaml页面。如何做到这一点。你能给我提供事件处理程序的代码吗?你能给我提供我可以解决上述问题的代码或链接吗?如果我做错了什么,请指导我。如果有比我的上述问题更好的想法,请提出建议

您可以用网格包裹文本块,并为其提供透明颜色。然后将NavigateToPageAction附加到网格,类似这样

xmlns:Custom="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:ic="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"

<Grid Background="Transparent">
    <Custom:Interaction.Triggers>
        <Custom:EventTrigger EventName="MouseLeftButtonDown">
            <ic:NavigateToPageAction TargetPage="/Views/YourView.xaml" />
        </Custom:EventTrigger>
    </Custom:Interaction.Triggers>
<TextBlock Foreground="Red" Text="{Binding CompanyORIndividual}" HorizontalAlignment="Left"/>
</Grid>
xmlns:Custom=“clr命名空间:System.Windows.Interactivity;assembly=System.Windows.Interactivity”
xmlns:ic=“clr命名空间:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions”
需要为网格指定颜色的原因是,这样做将能够接收鼠标单击。请尝试一下,如果你在使用上有问题,请告诉我

或者,您可以使用按钮包装文本块,然后处理按钮的单击事件

更新:

马特说得对,使用MouseLeftButtonDown/MouseLeftButtonUp有时可能不好

在我自己的项目中,我为一个按钮创建了一个简单的样式,它就像一个可点击的文本块。使用按钮的另一个好处是它可以接收倾斜效果并允许命令

<Style x:Key="TextButtonStyle" TargetType="Button">
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="BorderBrush" Value="{StaticResource PhoneForegroundBrush}"/>
    <Setter Property="Foreground" Value="{StaticResource PhoneAccentBrush}"/>
    <Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/>
    <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilySemiLight}"/>
    <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeLarge}"/>
    <Setter Property="Padding" Value="{StaticResource PhoneTouchTargetOverhang}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Grid Background="Transparent">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal"/>
                            <VisualState x:Name="MouseOver"/>
                            <VisualState x:Name="Pressed"/>
                            <VisualState x:Name="Disabled">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="HorizontalAlignment" Value="Left"/>
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    <Setter Property="VerticalContentAlignment" Value="Stretch"/>
</Style>

您可以用网格包裹文本块,并为其提供透明颜色。然后将NavigateToPageAction附加到网格,类似这样

xmlns:Custom="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:ic="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"

<Grid Background="Transparent">
    <Custom:Interaction.Triggers>
        <Custom:EventTrigger EventName="MouseLeftButtonDown">
            <ic:NavigateToPageAction TargetPage="/Views/YourView.xaml" />
        </Custom:EventTrigger>
    </Custom:Interaction.Triggers>
<TextBlock Foreground="Red" Text="{Binding CompanyORIndividual}" HorizontalAlignment="Left"/>
</Grid>
xmlns:Custom=“clr命名空间:System.Windows.Interactivity;assembly=System.Windows.Interactivity”
xmlns:ic=“clr命名空间:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions”
需要为网格指定颜色的原因是,这样做将能够接收鼠标单击。请尝试一下,如果你在使用上有问题,请告诉我

或者,您可以使用按钮包装文本块,然后处理按钮的单击事件

更新:

马特说得对,使用MouseLeftButtonDown/MouseLeftButtonUp有时可能不好

在我自己的项目中,我为一个按钮创建了一个简单的样式,它就像一个可点击的文本块。使用按钮的另一个好处是它可以接收倾斜效果并允许命令

<Style x:Key="TextButtonStyle" TargetType="Button">
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="BorderBrush" Value="{StaticResource PhoneForegroundBrush}"/>
    <Setter Property="Foreground" Value="{StaticResource PhoneAccentBrush}"/>
    <Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/>
    <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilySemiLight}"/>
    <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeLarge}"/>
    <Setter Property="Padding" Value="{StaticResource PhoneTouchTargetOverhang}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Grid Background="Transparent">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal"/>
                            <VisualState x:Name="MouseOver"/>
                            <VisualState x:Name="Pressed"/>
                            <VisualState x:Name="Disabled">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="HorizontalAlignment" Value="Left"/>
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    <Setter Property="VerticalContentAlignment" Value="Stretch"/>
</Style>


我建议在文本块上使用
轻触
手势。这将避免在用户滚动列表框时错误检测鼠标按钮向上或向下事件的问题。在我看来,这也比在列表框中检测SelectedItem更改提供了更好的用户体验

我建议在文本块上使用
点击
手势。这将避免在用户滚动列表框时错误检测鼠标按钮向上或向下事件的问题。在我看来,这也比在列表框中检测SelectedItem更改提供了更好的用户体验

下面是我使用VS2010所做的

  • 在xaml窗口中选择TextBlock控件
  • 在属性窗口中,选择事件
  • 找到
    MouseLeftButtonUp
    ,然后双击空值文本框,这将引导您进入xaml.cs代码隐藏窗口,并将创建一个新方法
  • 放置
    this.Close()在该方法中

  • 下面是我使用VS2010所做的

  • 在xaml窗口中选择TextBlock控件
  • 在属性窗口中,选择事件
  • 找到
    MouseLeftButtonUp
    ,然后双击空值文本框,这将引导您进入xaml.cs代码隐藏窗口,并将创建一个新方法
  • 放置
    this.Close()在该方法中

  • 作为我在WP8(Silverlight)中遇到的类似问题的解决方法,我使用了一个具有透明背景的按钮和一个Click事件处理程序

    现在的问题是,每当用户触摸/点击