2017-10-24 3 views
2

Ich versuche, einen Text von meinem ComboBox zu bekommen, um es zu wechseln, aber es gibt immer null. Was mache ich falsch?ComboBox.Text zeigt immer 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# Code:

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; 
      } 

Bin ich etwas fehlt? Danke für jede Art von Hilfe!

+1

Check this out: https://stackoverflow.com/questions/2961118/combobox-selectionchanged-event-has-old-value-not-new-value – praty

+3

gibt es einen Grund, warum Sie nicht usi ng 'Binding'? – XAMlMAX

Antwort

1

Dies sollte funktionieren:

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; 
        } 
      } 
     } 
    } 
} 

Wenn Sie es möchten, zunächst arbeiten, bevor Sie ein beliebiges Element ausgewählt haben, können Sie die SelectedIndex Eigenschaft des ComboBox anstelle des Einstellens der IsSelected Eigenschaft des ComboBoxItem setzen soll

<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

Aber es wird nicht von der ersten Auswahl arbeiten ('wird item.Content' null sein, während wirklich' Polygon' Element ausgewählt ist). – Evk

+0

Der erste Aufruf der Ereignisbehandlungsroutine geschieht aufgrund 'IsSelected = true' eines combox Artikel. Während dieses Aufrufs ist 'Content' wahrscheinlich 'null', wahrscheinlich aufgrund der Art und Weise, wie die Ansicht aufgebaut ist. Dies muss getrennt behandelt werden, denke ich. Eine andere Sache: 'cbForm' und' item' Null-Checks, sie werden nicht benötigt. – Sinatr

+0

Das ist nur eine Frage der Einstellung der SelectedIndex-Eigenschaft. Siehe meine Bearbeitung. – mm8