Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

So far i cannot explain the observed behavior, but found a relatively simple workaround:

private void    cbAdmDvc_SelectionChanged( object sender, SelectionChangedEventArgs e )
{
    if(  cbAdmDvc.SelectedIndex < 0  )  return;

    DvcType tiDvc;      /// add this temp variable to capture .SelectedValue

    if(  cbAdmDvc.SelectedValue != null  )
        tiDvc=  (DvcType) cbAdmDvc.SelectedValue;
    else
        tiDvc=  ((DvcTypes) cbAdmDvc.SelectedItem).tiDvcType;

    DoSmth( tiDvc );    /// instead of DoSmth( (DvcType)cbAdmDvc.SelectedValue )
}

Silly, but it works, since .SelectedItem is set properly.
As i said before, this is the only ComboBox experiencing such oddities out of several..

EDIT, 2014-Oct-21:

After making some changes in the application logic surprisingly found myself looking at the same issue with another ComboBox. Found a potential solution combobox-selectedvalue-not-updating-from-binding-sourcecombobox-selectedvalue-not-updating-from-binding-source, but when i tried setting initial values via .SelectedItem instead of .SelectedValue things got even weirder/worse. So i tried to apply my previous solution here as well and it worked!

Here's my attempt to explain the observed behavior:
Setting an initial value in code (CBox.SelectedValue= smth;) triggers CBox_SelectionChanged event. For some reason at that moment reading .SelectedValue returns null (as if it's not ready yet), however reading .SelectedItem seems to work fine! Once you're out of CBox_SelectionChanged event code can read .SelectedValue properly..

So, if you 1) have a handler for _SelectionChanged event, 2) refer to .SelectedValue within it, and 3) are setting initial choice via .SelectedValue somewhere else in code - watch out for null and code defensively! HTH!! :)

So far i cannot explain the observed behavior, but found a relatively simple workaround:

private void    cbAdmDvc_SelectionChanged( object sender, SelectionChangedEventArgs e )
{
    if(  cbAdmDvc.SelectedIndex < 0  )  return;

    DvcType tiDvc;      /// add this temp variable to capture .SelectedValue

    if(  cbAdmDvc.SelectedValue != null  )
        tiDvc=  (DvcType) cbAdmDvc.SelectedValue;
    else
        tiDvc=  ((DvcTypes) cbAdmDvc.SelectedItem).tiDvcType;

    DoSmth( tiDvc );    /// instead of DoSmth( (DvcType)cbAdmDvc.SelectedValue )
}

Silly, but it works, since .SelectedItem is set properly.
As i said before, this is the only ComboBox experiencing such oddities out of several..

EDIT, 2014-Oct-21:

After making some changes in the application logic surprisingly found myself looking at the same issue with another ComboBox. Found a potential solution combobox-selectedvalue-not-updating-from-binding-source, but when i tried setting initial values via .SelectedItem instead of .SelectedValue things got even weirder/worse. So i tried to apply my previous solution here as well and it worked!

Here's my attempt to explain the observed behavior:
Setting an initial value in code (CBox.SelectedValue= smth;) triggers CBox_SelectionChanged event. For some reason at that moment reading .SelectedValue returns null (as if it's not ready yet), however reading .SelectedItem seems to work fine! Once you're out of CBox_SelectionChanged event code can read .SelectedValue properly..

So, if you 1) have a handler for _SelectionChanged event, 2) refer to .SelectedValue within it, and 3) are setting initial choice via .SelectedValue somewhere else in code - watch out for null and code defensively! HTH!! :)

So far i cannot explain the observed behavior, but found a relatively simple workaround:

private void    cbAdmDvc_SelectionChanged( object sender, SelectionChangedEventArgs e )
{
    if(  cbAdmDvc.SelectedIndex < 0  )  return;

    DvcType tiDvc;      /// add this temp variable to capture .SelectedValue

    if(  cbAdmDvc.SelectedValue != null  )
        tiDvc=  (DvcType) cbAdmDvc.SelectedValue;
    else
        tiDvc=  ((DvcTypes) cbAdmDvc.SelectedItem).tiDvcType;

    DoSmth( tiDvc );    /// instead of DoSmth( (DvcType)cbAdmDvc.SelectedValue )
}

Silly, but it works, since .SelectedItem is set properly.
As i said before, this is the only ComboBox experiencing such oddities out of several..

EDIT, 2014-Oct-21:

After making some changes in the application logic surprisingly found myself looking at the same issue with another ComboBox. Found a potential solution combobox-selectedvalue-not-updating-from-binding-source, but when i tried setting initial values via .SelectedItem instead of .SelectedValue things got even weirder/worse. So i tried to apply my previous solution here as well and it worked!

Here's my attempt to explain the observed behavior:
Setting an initial value in code (CBox.SelectedValue= smth;) triggers CBox_SelectionChanged event. For some reason at that moment reading .SelectedValue returns null (as if it's not ready yet), however reading .SelectedItem seems to work fine! Once you're out of CBox_SelectionChanged event code can read .SelectedValue properly..

So, if you 1) have a handler for _SelectionChanged event, 2) refer to .SelectedValue within it, and 3) are setting initial choice via .SelectedValue somewhere else in code - watch out for null and code defensively! HTH!! :)

outlined conditions for observing behavior, added probable explanation, expanded code sample how to handle
Source Link
Astrogator
  • 1.1k
  • 2
  • 12
  • 29

So far i cannot explain the observed behavior, but found a relatively simple workaround:

private void    cbAdmDvc_SelectionChanged( object sender, SelectionChangedEventArgs e )
{
    if(  cbAdmDvc.SelectedIndex >=< 0  )  return;

    {
DvcType tiDvc;      /// add this temp variable to capture .SelectedValue

    if(  cbAdmDvc.SelectedValue != null  )
            tiDvc=  (DvcType) cbAdmDvc.SelectedValue;
        else
            tiDvc=  ((J790M.DAL.DvcTypes) cbAdmDvc.SelectedItem).tiDvcType; 

    }DoSmth( tiDvc );    /// instead of DoSmth( (DvcType)cbAdmDvc.SelectedValue )
}

Silly, but it works, since .SelectedItem is set properly.
As i said before, this is the only ComboBox experiencing such oddities out of several..

EDIT, 2014-Oct-21:

After making some changes in the application logic surprisingly found myself looking at the same issue with another ComboBox. Found a potential solution combobox-selectedvalue-not-updating-from-binding-source, but when i tried setting initial values via .SelectedItem instead of .SelectedValue things got even weirder/worse. So i tried to apply my previous solution here as well and it worked!

Here's my attempt to explain the observed behavior:
Setting an initial value in code (CBox.SelectedValue= smth;) triggers CBox_SelectionChanged event. For some reason at that moment reading .SelectedValue returns null (as if it's not ready yet), however reading .SelectedItem seems to work fine! Once you're out of CBox_SelectionChanged event code can read .SelectedValue properly..

So, if you 1) have a handler for _SelectionChanged event, 2) refer to .SelectedValue within it, and 3) are setting initial choice via .SelectedValue somewhere else in code - watch out for null and code defensively! HTH!! :)

So far i cannot explain the observed behavior, but found a relatively simple workaround:

private void    cbAdmDvc_SelectionChanged( object sender, SelectionChangedEventArgs e )
{
    if(  cbAdmDvc.SelectedIndex >= 0  )
    {
        if(  cbAdmDvc.SelectedValue != null  )
            tiDvc=  (DvcType) cbAdmDvc.SelectedValue;
        else
            tiDvc=  ((J790M.DAL.DvcTypes) cbAdmDvc.SelectedItem).tiDvcType;
    }
}

Silly, but it works, since .SelectedItem is set properly.
As i said before, this is the only ComboBox experiencing such oddities out of several..

So far i cannot explain the observed behavior, but found a relatively simple workaround:

private void    cbAdmDvc_SelectionChanged( object sender, SelectionChangedEventArgs e )
{
    if(  cbAdmDvc.SelectedIndex < 0  )  return;

    DvcType tiDvc;      /// add this temp variable to capture .SelectedValue

    if(  cbAdmDvc.SelectedValue != null  )
        tiDvc=  (DvcType) cbAdmDvc.SelectedValue;
    else
        tiDvc=  ((DvcTypes) cbAdmDvc.SelectedItem).tiDvcType; 

    DoSmth( tiDvc );    /// instead of DoSmth( (DvcType)cbAdmDvc.SelectedValue )
}

Silly, but it works, since .SelectedItem is set properly.
As i said before, this is the only ComboBox experiencing such oddities out of several..

EDIT, 2014-Oct-21:

After making some changes in the application logic surprisingly found myself looking at the same issue with another ComboBox. Found a potential solution combobox-selectedvalue-not-updating-from-binding-source, but when i tried setting initial values via .SelectedItem instead of .SelectedValue things got even weirder/worse. So i tried to apply my previous solution here as well and it worked!

Here's my attempt to explain the observed behavior:
Setting an initial value in code (CBox.SelectedValue= smth;) triggers CBox_SelectionChanged event. For some reason at that moment reading .SelectedValue returns null (as if it's not ready yet), however reading .SelectedItem seems to work fine! Once you're out of CBox_SelectionChanged event code can read .SelectedValue properly..

So, if you 1) have a handler for _SelectionChanged event, 2) refer to .SelectedValue within it, and 3) are setting initial choice via .SelectedValue somewhere else in code - watch out for null and code defensively! HTH!! :)

Source Link
Astrogator
  • 1.1k
  • 2
  • 12
  • 29

So far i cannot explain the observed behavior, but found a relatively simple workaround:

private void    cbAdmDvc_SelectionChanged( object sender, SelectionChangedEventArgs e )
{
    if(  cbAdmDvc.SelectedIndex >= 0  )
    {
        if(  cbAdmDvc.SelectedValue != null  )
            tiDvc=  (DvcType) cbAdmDvc.SelectedValue;
        else
            tiDvc=  ((J790M.DAL.DvcTypes) cbAdmDvc.SelectedItem).tiDvcType;
    }
}

Silly, but it works, since .SelectedItem is set properly.
As i said before, this is the only ComboBox experiencing such oddities out of several..