2014-01-09 4 views
1

wrappanel 및 listbox를 사용하여 wp7에 내 항목을 표시했습니다. 항목 클릭 이벤트가 작동하지 않습니다. 다음과 같이 내 코드는wrappanel 목록 상자 항목 선택 이벤트

<Grid x:Name="ContentPanel" Grid.Row="1" Height="Auto"> 
      <ListBox x:Name="lstDevice"> 
       <ListBox.ItemsPanel> 
        <ItemsPanelTemplate> 
         <toolkit:WrapPanel/>       
        </ItemsPanelTemplate> 
       </ListBox.ItemsPanel> 
       <ListBox.ItemTemplate> 
        <DataTemplate > 
         <StackPanel> 
          <Button x:Name="btnData" > 
           <StackPanel Orientation="Vertical"> 
            <Canvas 
              Width="175" 
              Height="175"/>            
            <TextBlock Text="{Binding Name}" Width="175" /> 
           </StackPanel> 
          </Button> 
         </StackPanel> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 
     </Grid> 

위의 디자인 코드와 C# 코드가 번호 또는 선택된 항목을 인식하는 몇 가지 속성을 목록 상자 항목 선택 이벤트를 생성하고 활용하는 방법 아래

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) 
     { 
      lstDevice.ItemsSource = MainPage.user.dArray.ToList();    
      lstDevice.SelectionChanged += item_Select; 
     } 

     private void item_Select(object sender, SelectionChangedEventArgs e) 
     { 
      int p = ((ListBox)sender).SelectedIndex; 
     } 

이다? 미리 감사드립니다!

답변

1

은 더 나은이 수도 정장을 생각 :

<Grid x:Name="ContentPanel" Grid.Row="1" Height="Auto"> 
      <ListBox x:Name="lstDevice"> 
       <ListBox.ItemsPanel> 
        <ItemsPanelTemplate> 
         <toolkit:WrapPanel/>       
        </ItemsPanelTemplate> 
       </ListBox.ItemsPanel> 
       <ListBox.ItemTemplate> 
        <DataTemplate > 
          <Button x:Name="btnData" Click="OnButtonClick" Tag="{Binding Name}" > 
           <StackPanel Orientation="Vertical"> 
            <Canvas 
              Width="175" 
              Height="175"/>            
            <TextBlock Text="{Binding Name}" Width="175" /> 
           </StackPanel> 
          </Button> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 
     </Grid> 

    private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) 
    { 
     lstDevice.ItemsSource = MainPage.user.dArray.ToList();    
    } 

    private void OnButtonClick(object sender, RoutedEventArgs e) 
    { 
     Button b = (Button)sender; 
     var nameInTag=b.Tag.ToString(); 
    } 
+1

감사합니다! 친구! 작동합니다! – Kevan

+0

이제는 내 dArray에 lstDevice.ItemSource에 대한 정보가 있습니다. OnButtonClick 메서드 dArray의 어느 요소를 클릭했는지 알고 싶습니다. 어떻게 할 생각인가? – Kevan

+0

dArray 란 무엇입니까? 나는 람다 표현식을'var c = dArray.Where (x => x == nameInTag) .Select (x => x) .FirstOrDefault();' –

0
Make change in your xaml and cs code like this: 

<Grid x:Name="ContentPanel" Grid.Row="1" Height="Auto"> 
       <ListBox x:Name="lstDevice" SelectionChange="item_Select"> 
        <ListBox.ItemsPanel> 
         <ItemsPanelTemplate> 
          <toolkit:WrapPanel/>       
         </ItemsPanelTemplate> 
        </ListBox.ItemsPanel> 
        <ListBox.ItemTemplate> 
         <DataTemplate > 
          <StackPanel> 
           <Button x:Name="btnData" > 
            <StackPanel Orientation="Vertical"> 
             <Canvas 
               Width="175" 
               Height="175"/>            
             <TextBlock Text="{Binding Name}" Width="175" /> 
            </StackPanel> 
           </Button> 
          </StackPanel> 
         </DataTemplate> 
        </ListBox.ItemTemplate> 
       </ListBox> 
      </Grid> 


     private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) 
      { 
       lstDevice.ItemsSource = MainPage.user.dArray.ToList();    

      } 

      private void item_Select(object sender, SelectionChangedEventArgs e) 
      { 
       var selctedItem = lstDevice.SelectedItem as (Your list box's itmsource) 
      } 
+0

오류 : "SelectionChange"멤버가 인식되지 않거나 액세스 할 수 없습니다. – Kevan