2

This is a weird error. I'm binding an enum to a combo box and displaying the description attribute. I'm using the solution at WPF Binding a ListBox to an enum, displaying the Description Attribute. So the relevant part of my XAML is:

<Window.Resources>
    <local:EnumConverter x:Key="EnumConverter"/>
    <ObjectDataProvider MethodName="GetValues"
            ObjectType="{x:Type local:MyEnum}"
            x:Key="MyEnumValues">
        <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="local:MyEnum" />
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
</Window.Resources>
<ComboBox Name="MyComboBox" ItemsSource="{Binding Source={StaticResource MyEnumValues}}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Converter={StaticResource EnumConverter}}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

Then my code is:

public enum MyEnum
{
    [Description("foo")]
    Foo,
    [Description("bar")]
    Bar
}

public class EnumConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        FieldInfo field_info = value.GetType().GetField(value.ToString());
        object[] attributes = field_info.GetCustomAttributes(false);
        if (attributes.Length == 0)
            return value.ToString();
        else
        {
            DescriptionAttribute attribute = attributes[0] as DescriptionAttribute;
            return attribute.Description;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

Now the weird part. I start the program and select a value from the combo box (this step is important). All works as expected. Then I connect to the computer via remote desktop. Immediately I get a NullReferenceException on the first line of the Convert() function. The Type parameter is a string, but otherwise there is not much information to troubleshoot, and the call stack is empty.

Craig W
  • 4,390
  • 5
  • 33
  • 51
  • Add a debug statement there and tell us, it it the object or is it the value which is null? Then look at the stack trace and work backwards to find out why. What is the object type? – JWP Jun 21 '17 at 01:07
  • @JohnPeters The `value` argument to the `Convert()` function is null which is causing the exception. `EnumConverter.Convert()` is the only thing in the stack trace. – Craig W Jun 21 '17 at 01:13
  • Ok, convertors are called at WPF render time, so what is the Object type? Can you show the XAML part.... and the binding collection? – JWP Jun 21 '17 at 01:14
  • @JohnPeters All the important XAML and code is shown above. Anything I didn't include is just auto-generated boilerplate. – Craig W Jun 21 '17 at 01:20

2 Answers2

2

If I understand your description correctly, the program instance that throws the exception when you connect via RDP is the same instance that you started on the computer using a direct logged-in session. I.e. you first start the program while sitting at the computer, and then you take over that same user session via RDP and interact with the already running program.

Correct?

If so, then this is a normal behavior. Switching to the RDP connection causes the WPF program to lose all of its video resources, because it's no longer rendering to the local video card, but rather to the virtualized video driver used for RDP. As such, WPF has to rebuild the UI. In the process of doing this, your binding momentarily has a null value. The converter is called during this time, where you call ToString() without first checking for the null value, which results in the NullReferenceException.

Since you're unlikely to reliably force WPF to change its ways in the context of an RDP session, the only viable solution is to check value for a null value, and to do something reasonable in that case (e.g. return Binding.DoNothing;). Once WPF has settled back down, it should get back into a state where you have an actual value again and you'll be back to the normal state.

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
0

Your Static Resources has nothing in it. Or it cannot be found. Open your output window to see binding errors when this view comes up.

Binding Source={StaticResource MyEnumValues}}

Why? Because if you are getting null on ToString() below that most likely means that the value itself is null.

  Enum myEnum = (Enum)value;
  var stringValue = myEnum.ToString();
JWP
  • 6,672
  • 3
  • 50
  • 74
  • `MyEnumValues` is defined in the `ObjectDataProvider` section of the XAML. To clarify, everything works perfectly fine when I run the program and select an item from the combo box. The problem comes in _only_ when I then connect via remote desktop. – Craig W Jun 21 '17 at 01:29
  • Then it's not the program itself, rather it's environmental.. However, I don't see any reason this would fail just because of RDP. – JWP Jun 21 '17 at 13:11