2013-06-30 2 views
2
나는 목록 상자에서 항목을 선택 목록 상자를 사용하고 있지만 내가 그것을 선택으로는 불공정 exception.Here 내 코드를 생성

은 다음과 같습니다 `목록 상자가 선택 이벤트 오류

<ListBox Grid.Row="1" SelectionChanged="PrintText" Background="DarkGray" Visibility="Collapsed" Height="Auto" HorizontalAlignment="Left" Margin="156,36,0,0" Name="listBox1" VerticalAlignment="Top" Width="191" UseLayoutRounding="True" /> 

void PrintText(object sender, SelectionChangedEventArgs args) 
     { 
      ListBoxItem lbi = ((sender as ListBox).SelectedItem as ListBoxItem); 
      String a = lbi.Content.ToString(); 
      Window1 neww = null; 
      neww = new Window1(); 
      neww.Show(); 
     } 

`내가 돈 내가 실수를하고있는 곳을 안내 해줘. 나는 당신을 더 분명하게하기 위해 그것의 이미지를 첨부했습니다. enter image description here 감사합니다!

답변

1
void PrintText(object sender, SelectionChangedEventArgs args) 
{ 
    object item = listBox1.SelectedItem; 

    if (item == null) { 
    txtSelectedItem.Text = "No item currently selected."; 
    } else { 
    txtSelectedItem.Text = item.ToString(); 
    } 

    // ListBoxItem lbi = ((sender as ListBox).SelectedItem as ListBoxItem); 
    // String a = lbi.Content.ToString(); 
    Window1 neww = null; 
    neww = new Window1(); 
    neww.Show(); 
} 
+0

이 당신의 대답이 있다면, 친절하게 답으로 선택, 다른 사람이 설명없이 다시 코드의 – Shaharyar

+1

그냥 조각을 전체 문제를 읽을 수 없을 것이다는 유용하지 않습니다. – svick

관련 문제