C# 当ItemSource绑定到转换器时,SelectedValuePath在组合框中不起作用

C# 当ItemSource绑定到转换器时,SelectedValuePath在组合框中不起作用,c#,wpf,xaml,data-binding,C#,Wpf,Xaml,Data Binding,我的课程如下: public class VmUserNotification : BindableBaseThreadSafe { private string _username; private EnumLocalizer<NotificationLevel> _notificationLevel; private List<EnumLocalizer<NotificationLevel>> _notification

我的课程如下:

public class VmUserNotification : BindableBaseThreadSafe
{
  private string _username;
        private EnumLocalizer<NotificationLevel> _notificationLevel;
        private List<EnumLocalizer<NotificationLevel>> _notificationOptions;

 public string Username
        {
            get => _username;
            set => Set(ref _username, value);
        }

        public EnumLocalizer<NotificationLevel> NotificationLevel
        {
            get => _notificationLevel;
            set => Set(ref _notificationLevel, value);
        }

        public List<EnumLocalizer<NotificationLevel>> NotificationOptions
        {
            get => _notificationOptions;
            set => Set(ref _notificationOptions, value);
        }
}
foreach (var user in usersWithNotificationLevelsOtherThanNone)
                {
                    var allOptions = new List<EnumLocalizer<NotificationLevel>>();
                    Enum.GetValues(typeof(NotificationLevel)).Cast<NotificationLevel>().ToList().ForEach(
                        logEventLevel => allOptions.Add(new EnumLocalizer<NotificationLevel>() { Value = logEventLevel }));

                    AlertSettings.UserNotificationListWithAllRoles.Add(new VmUserNotificationWithAllRoles()
                    {
                        Username = user.UserName,
                        NotificationOptions = allOptions,
                        NotificationLevel = allOptions.FirstOrDefault(x => x.Value == user.Notifications)
                    });

                }
在ViewModel中,我有一个VmUserNotificationWithAllRoles的ObservableCollection。我将其填充如下:

public class VmUserNotification : BindableBaseThreadSafe
{
  private string _username;
        private EnumLocalizer<NotificationLevel> _notificationLevel;
        private List<EnumLocalizer<NotificationLevel>> _notificationOptions;

 public string Username
        {
            get => _username;
            set => Set(ref _username, value);
        }

        public EnumLocalizer<NotificationLevel> NotificationLevel
        {
            get => _notificationLevel;
            set => Set(ref _notificationLevel, value);
        }

        public List<EnumLocalizer<NotificationLevel>> NotificationOptions
        {
            get => _notificationOptions;
            set => Set(ref _notificationOptions, value);
        }
}
foreach (var user in usersWithNotificationLevelsOtherThanNone)
                {
                    var allOptions = new List<EnumLocalizer<NotificationLevel>>();
                    Enum.GetValues(typeof(NotificationLevel)).Cast<NotificationLevel>().ToList().ForEach(
                        logEventLevel => allOptions.Add(new EnumLocalizer<NotificationLevel>() { Value = logEventLevel }));

                    AlertSettings.UserNotificationListWithAllRoles.Add(new VmUserNotificationWithAllRoles()
                    {
                        Username = user.UserName,
                        NotificationOptions = allOptions,
                        NotificationLevel = allOptions.FirstOrDefault(x => x.Value == user.Notifications)
                    });

                }
foreach(usersWithNotificationLevelsOtherThanNone中的var用户)
{
var allOptions=新列表();
Enum.GetValues(typeof(NotificationLevel)).Cast().ToList().ForEach(
logEventLevel=>allOptions.Add(新的EnumLocalizer(){Value=logEventLevel}));
AlertSettings.UserNotificationListWithAllRoles.Add(新建VmUserNotificationWithAllRoles())
{
Username=user.Username,
NotificationOptions=allOptions,
NotificationLevel=allOptions.FirstOrDefault(x=>x.Value==user.Notifications)
});
}
在UI中,我使用转换器将组合框的itemsSource绑定到NotificationOptions。由于NotificationOptions将是一个可观察的列表集合,所以我有一个转换器。这是组合框

<ComboBox Margin="{StaticResource MarginAfterText}" 
                                              x:Name="NotificationsComboBox" 
                                              Grid.Column="2"
                                              ItemsSource="{Binding AlertSettings.UserNotificationListWithAllRoles,Converter={StaticResource TestConverter}}" 
                                              SelectedValuePath="NotificationLevel"
                                              Width="200">

这是我的转换器

public class TestConverter:IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if(value is ObservableCollection<VmUserNotificationWithAllRoles> vmUserNotifications)
            {
                var item = vmUserNotifications.FirstOrDefault();
                if (item != null)
                {
                    return item.NotificationOptions;
                }
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
公共类TestConverter:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、字符串语言)
{
if(值为ObservableCollection vmUserNotifications)
{
var item=vmUserNotifications.FirstOrDefault();
如果(项!=null)
{
返回项。通知选项;
}
}
返回null;
}
公共对象转换回(对象值、类型targetType、对象参数、字符串语言)
{
抛出新的NotImplementedException();
}
}

问题是组合框没有选择值,它是空的。请帮忙。保留断点时,我可以看到NotificationLevel的值。

SelectedValuePath
仅与
SelectedValuePath
结合使用

例如,如果您有一个item类,如

public class Item
{
    string Value { get; set; }
}
具有如下视图模型

public class ViewModel : INotifyPropertyChanged
{
    public ObservableCollection<Item> Items { get; } = new ObservableCollection<Item>();

    public string SelectedItemValue { get; set; } // must fire PropertyChanged
}
公共类视图模型:INotifyPropertyChanged
{
公共ObservableCollection项{get;}=new ObservableCollection();
公共字符串SelectedItemValue{get;set;}//必须激发PropertyChanged
}
还有一个ItemsControl,如

<ItemsControl ItemsSource="{Binding Items}"
              SelectedValue="{Binding SelectedItemValue}"
              SelectedValuePath="Value">
    ...
</ItemsControl>

...
SelectedItemValue
属性设置为,例如
“Test”
将选择
值设置为
“Test”
的项目