2013-02-03 44 views
0

對於我在windows phone中的列表框,我在this tutorial(選項2)的幫助下爲每個項目添加了一個複選框。在我的列表框中已經有一個SelectionChanged事件之前。如何防止觸發事件,當用戶只檢查複選框時? SelectionChanged事件只應觸發列表框中的文本框,而不是複選框。列表框中的訪問控制,而不觸發selectionChanged事件

這就是我的列表框:

   <ListBox x:Name="toDoItemsListBox" ItemsSource="{Binding Source={StaticResource cvs}}" Grid.Row="1" Margin="12, 0, 12, 0" Width="440" SelectionChanged="goToNavigation" IsSynchronizedWithCurrentItem="False"> 
        <ListBox.ItemTemplate> 
         <DataTemplate> 
          <StackPanel Orientation="Horizontal" > 
          <CheckBox IsChecked="{Binding IsFavorite}" Height="48" Width="48" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked" Style="{StaticResource CheckBoxStyle1}"/> 
          <StackPanel Orientation="Vertical"> 
           <TextBlock Text="{Binding Shortcut}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" /> 
           <TextBlock Text="{Binding BuildingName}" TextWrapping="Wrap" Style="{StaticResource PhoneTextSubtleStyle}" /> 
          </StackPanel> 
         </StackPanel> 
        </DataTemplate> 
        </ListBox.ItemTemplate> 
       </ListBox> 

這是我的檢查項目:

 private void CheckBox_Checked(object sender, RoutedEventArgs e) 
    { 
     ListBoxItem checedItem = this.toDoItemsListBox.ItemContainerGenerator.ContainerFromItem((sender as CheckBox).DataContext) as ListBoxItem; 
     if (checedItem != null) 
     { 
      checedItem.IsSelected = true; 
     } 
    } 

    private void CheckBox_Unchecked(object sender, RoutedEventArgs e) 
    { 
     ListBoxItem checedItem = this.toDoItemsListBox.ItemContainerGenerator.ContainerFromItem((sender as CheckBox).DataContext) as ListBoxItem; 
     if (checedItem != null) 
     { 
      checedItem.IsSelected = false; 
     } 
    } 

此外,我不得不提的是,當我選中該複選框SelectionChanged事件僅提高。不是當我取消選中。

回答

0

我讓它在沒有選擇更改事件的情況下工作。作爲備選方案,我用了一個點擊事件:

      <StackPanel Orientation="Vertical" Tap="StackPanel_Tap"> 
           <TextBlock Text="{Binding Shortcut}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" /> 
           <TextBlock Text="{Binding BuildingName}" TextWrapping="Wrap" Style="{StaticResource PhoneTextSubtleStyle}" /> 
          </StackPanel> 

並在後面的代碼讓我的列表項有:

 private void StackPanel_Tap(object sender, System.Windows.Input.GestureEventArgs e) 
    { 
     // Save current POI. 
     Object object = (sender as StackPanel).DataContext as Object; 
    ...} 
相關問題