【问题标题】:How to add the click event on textblock of listbox?如何在列表框的文本块上添加点击事件?
【发布时间】:2011-04-29 14:04:53
【问题描述】:

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

<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>

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

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

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

【问题讨论】:

  • 您真的在 PivotItem 中放置了水平滚动列表框吗?这可能会导致列表框上的手势被误解为枢轴项目上的手势的问题,反之亦然。通常不建议这样做,您应该与实际用户彻底测试。

标签: windows-phone-7 xaml listbox onclick textblock


【解决方案1】:

你可以用一个网格包裹 TextBlock,并给它一个透明的颜色。然后你将一个 NavigateToPageAction 附加到 Grid,像这样,

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>

您需要给网格一个颜色的原因是因为这样做将能够接收鼠标点击。请试一试,如果您在使用时遇到问题,请告诉我。

或者,您可以使用 Button 包装 TextBlock,然后处理 Button 的单击事件。

更新:

关于使用 MouseLeftButtonDown/MouseLeftButtonUp 有时可能不好的事情,Matt 是对的。

在我自己的项目中,我为按钮创建了一个简单的样式,其作用类似于可点击的 TextBlock。使用按钮的另一个好处是它可以接收 TiltEffect 并允许指挥。

<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>

【讨论】:

    【解决方案2】:

    我建议在 TextBlock 上使用 Tap 手势(来自 Silverlight Toolkit)。这将避免在用户滚动列表框时错误地检测鼠标按钮向上或向下事件的问题。在我看来,这也提供了比检测 Listbox 上更改的 SelectedItem 更好的用户体验。

    【讨论】:

    • +1,我认为你是对的,我会更新我的答案以使用按钮。点击和按钮点击应该是一样的吧?
    【解决方案3】:

    这是我使用 VS2010 所做的。

    1. 在 xaml 窗口中选择 TextBlock 控件。
    2. 在属性窗口中,选择事件。
    3. 找到MouseLeftButtonUp,然后双击空值文本框,将引导您进入窗口后面的xaml.cs代码,并创建一个新方法。
    4. this.Close(); 放入该方法中。

    【讨论】:

      【解决方案4】:

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

      现在的问题是,每当用户触摸/单击按钮时,都会将纯色 PhoneAccent 颜色设置为按钮的背景(在用户按住它的时间段内)。

      为避免这种情况,您可以设置按钮的ClickMode="Pressed" 并在Click 事件中使用MyButton.Background = new SolidColorBrush(Colors.Transparent); 将背景设置为透明

      当 Button 处于 Pressed 状态时触发 Click 事件时,背景设置为透明,因此当处于该状态时,背景颜色设置为 Button。当Button处于按下状态时,不设置ClickMode="Pressed"不会设置背景颜色。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-12-26
        • 2015-01-01
        • 2017-09-29
        • 1970-01-01
        • 1970-01-01
        • 2019-11-15
        • 2013-07-10
        相关资源
        最近更新 更多