开发者

Combobox still has old value in SelectionChanged event

I have question about the combo box. The problem I'm having is with the selection changed event I'm binding to it. Here is my Code:

        <ComboBox Height="23"
              HorizontalAlignment="Left"
              Margin="12,67,0,0"
              Name="comboBox1"
              VerticalAlignment="Top"
              Width="112"
              SelectionChanged="comboBox1_SelectionChanged">
        <ComboBoxItem Content="Pokey"
                      IsSelected="True" />
        <ComboBoxItem Content="Octo-ooze" />
        <ComboBoxItem Content="Bolt" />
        <ComboBoxItem Content="Fink" />
    </ComboBox>

And in the code behind:

    private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

        switch (comboBox1.Text)
        {
        case "Pokey" :
        tbMinutes.Text = "0";
        tbSeconds.Text = "8";
        break;
        case "Octo-ooze":
        tbMinutes.Text = "0";
        tbSeconds.Text = "16";
        break;
        case "Bolt":
        tbMinutes.Text = "0";
        tbSeconds.Text = "23";
        break;
        case "Fink":
        tbMinutes.Text = "1";
        tbSeconds.Text = "40";
        break;
        }
    }

What is happening is tbMinutes.Text and开发者_如何学JAVA tbSeconds.Text uses the old values, so for example if the combo box is initially Pokey and I change it to Bolt, it will still use the Pokey values as if its lagging one step behind.


The comboBox1.Text returns the value prior to the selection change, which is what is causing your problem. You can use ComboBox.SelectedItem to access the newly selected item.

The change would look something like this

private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
  ComboBoxItem item = comboBox1.SelectedItem as ComboBoxItem;
  if (item != null)
  {
    switch (item.Content.ToString())
    {
      // ... Rest of your code here
    }
  }
}

However, you potentially have a problem, because the SelectionChanged event is fired when the data is loaded because you have set IsSelected="True" in you Xaml, if the textboxes are not yet created this will cause problem when the textbox values are initially set. To get around this, remove the IsSelected setting from the Xaml and just set the selection in the Load event of the Window.

private void Window_Loaded(object sender, RoutedEventArgs e)
{
  comboBox1.SelectedIndex = 0;
}  


In this case you should use the ComboBox.SelectedItem like mentioned by Chris Taylor. But be careful with setting the IsSelected Property of the ComboBoxItem in the XAML, because if you do it like this:

<ComboBox Height="23"
          HorizontalAlignment="Left"
          Margin="12,67,0,0"
          Name="comboBox1"
          VerticalAlignment="Top"
          Width="112"
          SelectionChanged="comboBox1_SelectionChanged">
        <ComboBoxItem Content="Pokey"
                  IsSelected="True" />
        <ComboBoxItem Content="Octo-ooze" />
        <ComboBoxItem Content="Bolt" />
        <ComboBoxItem Content="Fink" />
</ComboBox>
    <TextBox Name="tbMinutes" />
    <TextBox Name="tbSeconds" />

You will get a NullReferenceException, because when InitializeComponents is called, the components are initalized in the order defined in the XAML. If your first ComboBoxItem is initialized, a event is fired. But the TextBoxes are not initialized and null leading to the Exception.

One approach to solve the problem would be if you put the TextBoxes before the ComboBox in the XAML, but a much better approach would be if you hook up the event of the ComboBox in the code like this:

public MainWindow()
{
    InitializeComponent();
    comboBox1.SelectionChanged += comboBox1_SelectionChanged;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜