2011-04-29 55 views
1

我正在开发window 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> 

此列表框位于pivot控件中。我想将链接或点击事件添加到以下文本块中。

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

因此,在点击CompanyORIndividualblocks后,用户应该被导航到另一个xaml页面。这个怎么做。你能否给我提供事件处理程序的代码?你能否提供我可以通过哪些代码或链接来解决上述问题?如果我做错了任何事情,请指导我。如果有比我上面的问题更好的主意,那么请提出建议。

+2

你真的把一个水平滚动列表框内的PivotItem?这可能会导致列表框上的手势问题被误解为数据透视表上的手势,反之亦然。这是通常不推荐的,你应该用实际的用户进行彻底的测试。 – 2011-04-29 17:58:25

回答

4

你可以用网格包装TextBlock,并给它一个透明的颜色。然后您将一个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> 

,你需要给电网中的颜色是因为这样做将能接收鼠标点击的原因。请尝试一下,让我知道如果你有使用它的问题。

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

更新:

马特是正确的事儿,使用的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> 
1

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

+0

+1,我认为你的权利,我会更新我的答案,而不是使用按钮。点击和按钮点击应该以相同的方式工作吗? – 2011-05-01 07:56:52

1

这就是我使用VS2010所做的。

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

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

现在的问题是,无论何时用户触摸/点击按钮,都会将固定的PhoneAccent颜色设置为按钮的背景(用户持有它的时间段)。

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

背景设置为透明每当Click事件中按下状态按钮烧成,背景当它处于该状态时,颜色被设置为按钮。当按钮处于按下状态时,不设置ClickMode="Pressed"不会设置背景颜色。

相关问题