2017-10-24 1 views
2

전환을 위해 내 ComboBox에서 텍스트를 가져 오려고하는데, 항상 null을 반환합니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?ComboBox.Text에 항상 null이 표시됩니까?

XAML :

<ComboBox Name="cbForms" SelectionChanged="cbForms_SelectionChanged" HorizontalAlignment="Left" Margin="10,289,0,0" VerticalAlignment="Top" Width="139"> 
    <ComboBoxItem IsSelected="True">Polygon</ComboBoxItem> 
    <ComboBoxItem>Rechteck</ComboBoxItem> 
    <ComboBoxItem>Dreieck</ComboBoxItem> 
    <ComboBoxItem>Kreis</ComboBoxItem> 
</ComboBox> 

C# 코드 :

private void cbForms_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    string text = cbForms.Text; 
    switch (text) 
    { 
     case "Polygon": 
      { 
       commandText = "SELECT f.bezeichnung, t.X, t.Y, t.id FROM figure05 f, TABLE(SDO_UTIL.GETVERTICES(f.shape)) t"; 
       lblAnz.Content = anzPolygon.ToString(); 
       break; 
      } 

내가 실종 무엇인가? 도움 주셔서 감사합니다.

+1

확인이 아웃 : https://stackoverflow.com/questions/2961118/combobox-selectionchanged-event-has-old-value-not-new-value – praty

+3

는 USI 수없는 어떤 이유가 있습니까 바인드? – XAMlMAX

답변

1

이 작동합니다 :

private void cbForms_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    if (cbForms != null) 
    { 
     ComboBoxItem item = cbForms.SelectedItem as ComboBoxItem; 
     if (item != null && item.Content != null) 
     { 
      string text = item.Content.ToString(); 
      switch (text) 
      { 
       case "Polygon": 
        { 
         commandText = "SELECT f.bezeichnung, t.X, t.Y, t.id FROM figure05 f, TABLE(SDO_UTIL.GETVERTICES(f.shape)) t"; 
         lblAnz.Content = anzPolygon.ToString(); 
         break; 
        } 
      } 
     } 
    } 
} 

당신이 항목을 선택하기 전에 처음에 작업 할 경우, ComboBoxSelectedIndex 속성을 설정해야합니다 대신 ComboBoxItemIsSelected 속성을 설정하는 :

<ComboBox Name="cbForms" SelectionChanged="cbForms_SelectionChanged" HorizontalAlignment="Left" Margin="10,289,0,0" 
      VerticalAlignment="Top" Width="139" 
      SelectedIndex="0"> 
    <ComboBoxItem>Polygon</ComboBoxItem> 
    <ComboBoxItem>Rechteck</ComboBoxItem> 
    <ComboBoxItem>Dreieck</ComboBoxItem> 
    <ComboBoxItem>Kreis</ComboBoxItem> 
</ComboBox> 
+0

하지만 초기 선택에서는 작동하지 않습니다 ('Polygon' 항목이 선택되면 'item.Content'는 null이됩니다). – Evk

+0

첫 번째 이벤트 처리기 호출은 combox 항목 중 하나의 'IsSelected = true'로 인해 발생합니다. 이 콜을하는 동안 'Content'는 뷰 생성 방법 때문에 아마도 null입니다. 이것은 따로 처리해야합니다. 또 다른 한가지 :'cbForm'과'item' null-checks, 그들은 필요하지 않습니다. – Sinatr

+0

그것은 단지 SelectedIndex 속성을 설정하는 문제입니다. 내 편집을 참조하십시오. – mm8

관련 문제