2014-11-09 5 views
1

이 문제는 나를 미치게 만들었습니다. ListPicker에는 여러 항목이 동적으로 채워져 있습니다. 내 SelectionChanged 이벤트 처리기를 내 페이지의 Loaded 이벤트에 선언했습니다. 사용자가 페이지의 특정 항목을 클릭하면 ListPicker 가시성이 Collpased에서 Visible로 전환되고 ListPicker의 값을 설정합니다. 문제는 ListPicker의 인덱스가 사용자 설정을 기반으로하므로 세 항목 중 현재 인덱스가 기본값이 아닌 0이 아닌 1 일 수 있습니다. SelectionChanged 이벤트 실행 (현재 인덱스를 기반으로 작업을 수행함)없이 ListPicker의 현재 항목을 1로 표시해야합니다. 그런 다음에만 사용자가 선택한 항목을 직접 변경하면 SelectionChanged 이벤트가 필요합니다.ValueChanged 이벤트를 실행하지 않고 ListPicker의 인덱스를 설정하는 방법

주된 이유는 ListPicker가 표시되었을 때 ListPicker에서 현재 설정을 확인해야 할뿐만 아니라 현재 혼란 스럽거나 예상치 못한 항목을 덮어 쓰는 SelectionChanged 이벤트 작업에서 발생합니다. 사용자가 지정하지 않으면 발생합니다.

XAML

<toolkit:ListPicker x:Name="lp" Visibility="Collapsed" Margin="12" Width="300"/> 
을 다음과 같이

는 내가 현재 가지고 것은

XAML.CS

private void Page_Loaded(object sender, RoutedEventArgs e) 
    { 
     lp.SelectionChanged += lp_SelectionChanged; 
    } 

void EditableEllipse_Tap(object sender, System.Windows.Input.GestureEventArgs e) 
{ 
    if (sender != null) 
    { 
     DependencyObject tappedElement = e.OriginalSource as UIElement; 
     // find parent UI element of type PhotoThumbnail 
     //PhotoThumbnail i = this.FindParentOfType<PhotoThumbnail>(tappedElement); 
     i = this.FindParentOfType<PhotoThumbnail>(tappedElement); 
     if (i != null) 
     { 
      BuildControl(i); 
     } 
    } 
} 

private void BuildControl(PhotoThumbnail pp) 
    { 
     switch(pp.NName) 
     { 
      case "flip": 
       List<ListPickerItem> l = new List<ListPickerItem>();      
       l.Add(new ListPickerItem { Name = "Flip_Vertical", Content = AppResources.App_Flip_Vertical }); 
       l.Add(new ListPickerItem { Name = "Flip_Horizontal", Content = AppResources.App_Flip_Horizontal }); 
       l.Add(new ListPickerItem { Name = "Flip_Both", Content = AppResources.App_Flip_Both }); 
       lp.ItemsSource = l; //Code execution jumps from here to ValueChanged event immediately     
       lp.Visibility = Visibility.Visible; 
       lp.SelectedIndex = Settings.Flip.Value - 1; 
       break; 
     .. 
     } 
    } 

private async void lp_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
      if (lp.SelectedIndex != -1) //always defaults to = 0 
      { 
       var item = (sender as ListPicker).SelectedItem; 
       string name = ((ListPickerItem)item).Name; 

       if (name != null) 
       { 
         switch (name) 
         { 
          case "Flip_Vertical": 
           Settings.Flip.Value = 1; 
           ..perform process based on current Setting.Flip.Value..         break; 
          case "Flip_Horizontal": 
           Settings.Flip.Value = 2; 
           ..perform process based on current Setting.Flip.Value.. 
           break; 
          case "Flip_Both": 
           Settings.Flip.Value = 3; 
           ..perform process based on current Setting.Flip.Value.. 
           break; 

          ... 
          } 
       } 
      } 

답변

0

(이벤트를 unhooking 및 rehooking에 의해) 조작 기술의 순서를 사용해보십시오
private void BuildControl(PhotoThumbnail pp) 
{ 
    switch(pp.NName) 
    { 
     case "flip": 

      // unhook event 
      lp.SelectionChanged -= lp_SelectionChanged; 

      List<ListPickerItem> l = new List<ListPickerItem>();      
      l.Add(new ListPickerItem { Name = "Flip_Vertical", Content = AppResources.App_Flip_Vertical }); 
      l.Add(new ListPickerItem { Name = "Flip_Horizontal", Content = AppResources.App_Flip_Horizontal }); 
      l.Add(new ListPickerItem { Name = "Flip_Both", Content = AppResources.App_Flip_Both }); 
      lp.ItemsSource = l; //Code execution jumps from here to ValueChanged event immediately     
      lp.Visibility = Visibility.Visible; 
      lp.SelectedIndex = Settings.Flip.Value - 1; 

      // after setting the index, rehook the event 
      lp.SelectionChanged += lp_SelectionChanged; 

      break; 
    .. 
    } 
} 
+0

재미 있은 권리 마지막 선택으로이 글을 읽기 전에 같은 것을 시도해 보았습니다. 'Page_Loaded'에서'lp.SelectionChanged + = lp_SelectionChanged'를 제거 할 것을 권합니다. – Matthew

+1

@Matthew 당신이 Page_Loaded 다음에 트리거 할 다른 것이 있으면 달려있다. 아무것도 없다면 제거하는 것이 안전하다. –