如何在列表框的文本块上添加点击事件?

发布于 2024-11-04 06:26:14 字数 1550 浏览 0 评论 0原文

我正在开发 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 页面。如何做到这一点。您能给我提供事件处理程序的代码吗?您能给我提供可以解决上述问题的代码或链接吗?如果我做错了什么,请指导我。如果有比我上面的问题更好的想法,请提出建议。

I am developing window phone 7 application. I have a listbox in my application. I am doing dynamic binding with that listbox. The code of listbox is as follows:

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

This listbox is inside the pivot control. I want to add the link or click event one the following textblock.

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

So that after clicking on the CompanyORIndividual textblock the user should be navigated to the other xaml page. How to do this. Can you please provide me the code with the event handler ? Can you please provide me nay code or link through which I can resolve the above issue ? If I am doing anything wrong then please guide me.If there is any better idea than my above question then please suggest it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

栀梦 2024-11-11 06:26:14

您可以用网格包裹 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>

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

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

更新:

Matt 的说法是正确的,即使用 MouseLeftButtonDown/MouseLeftButtonUp 有时可能会很糟糕。

在我自己的项目中,我为按钮创建了一个简单的样式,其作用类似于可单击的 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>

you can wrap the TextBlock with a Grid, and give it a Transparent color. Then you attach a NavigateToPageAction to the Grid, something like this,

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>

The reason that you need to give the Grid a color is because doing so will be able to receive mouse click. Please give it a try and let me know if you have problems using it.

Alternatively, you can wrap the TextBlock with a Button, then handle the Button's click event.

Update:

Matt is right about the thing that using MouseLeftButtonDown/MouseLeftButtonUp may sometimes be bad.

In my own project I have created a simple style for a button which acts like a clickable TextBlock. Also another good thing of using a button is it can receive the TiltEffect and allow Commanding.

<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>
相对绾红妆 2024-11-11 06:26:14

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

I'd recommend using a Tap gesture (from the Silverlight Toolkit) on the TextBlock. This will avoid issues with incorrectly detecting mousebutton up or down events when the user is scrolling the listbox. In my opinion this also provides a better user experience than detecting SelectedItem changed on the Listbox.

走过海棠暮 2024-11-11 06:26:14

这是我使用 VS2010 所做的。

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

Here is what I did using VS2010.

  1. Select the TextBlock control in the xaml window.
  2. In the properties window, select Events.
  3. Locate MouseLeftButtonUp, and double-click in the empty value text box, that will lead you to the xaml.cs code behind window, and will create a new method.
  4. Place this.Close(); in that method.
凡尘雨 2024-11-11 06:26:14

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

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

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

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

As workaround for a similar problem I faced in WP8 (Silverlight), I used a Button with transparent background and a Click event handler.

Now the problem with this is that, whenever the user touches/clicks on the button, a solid PhoneAccent color is set as the button's background (for the time period user holds it).

To avoid this, you can set the button's ClickMode="Pressed" and in Click event set the background to transparent with MyButton.Background = new SolidColorBrush(Colors.Transparent);

Background is set to transparent whenever the Click event is fired with Button in Pressed state so the background color is set to Button when it is in that state. Not setting the ClickMode="Pressed" will not set Background color when Button is in pressed state.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文