绑定到ComboBox时发生WPF错误

时间:2012-12-06 09:31:40

标签: c# wpf combobox enums ivalueconverter

我有一个像下面这样的枚举:

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

}

我将它绑定到一个组合框作为itemsource,如下所示:

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

它工作正常,我想要的是从app.config文件获取值然后绑定到与SelectedItem相同的组合框

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

转换器的值是:

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

当我运行它时,组合框工作正常。但是当我在代码隐藏时调用它们时,其他控件变为空。

0 个答案:

没有答案