2011-04-29 54 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"不會設置背景顏色。

相關問題