0

I have a enum like following:

public enum ProcessControlSysType
{
    PFS = 1,
    MES = 2,

}

I bind it to a combobox as itemsource like following:

<ObjectDataProvider x:Key="ProcessSystemType"
                MethodName="GetValues" 
                ObjectType="{x:Type sys:Enum}">
        <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="local:ProcessControlSysType" />
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
<ComboBox Name="combProcessControlSysType" 
                      ItemsSource="{Binding Source={StaticResource ProcessSystemType }}" 
                      SelectionChanged="combProcessControlSysType_SelectionChanged"></ComboBox>

It is works fine, what i want is get value from app.config file then bind to same combobox as SelectedItem

<ComboBox Name="combProcessControlSysType" 
                      ItemsSource="{Binding Source={StaticResource ProcessSystemType }}" 
                      SelectedItem="{Binding Source={StaticResource ConfigFile}, Path=Default.ProcessControlSys, Converter={StaticResource ProcessSystemConverter}}"
                      SelectionChanged="combProcessControlSysType_SelectionChanged"></ComboBox>

The value Converter is :

public class ProcessSystemToStringConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {

        string strProcessSystem = value.ToString();

        switch (strProcessSystem)
        {
            case "PFS" :
                return ProcessControlSysType.PFS;
            case "MES" :
                return ProcessControlSysType.MES;

            default:
                return null;
        }
    }

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

When i run this, the combobox works fine. But other contorls become null when i call them at code-behind.

Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
LIU
  • 295
  • 1
  • 4
  • 11

0 Answers0