繁体   English   中英

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

[英]SelectedValuePath is not working in a combobox when ItemSource is bound to a Converter

我有一个类如下,

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

现在我有另一个类,如下所示,它实现了上述类,

 public class VmUserNotificationWithAllRoles : VmUserNotification
        {
        }

在 ViewModel 中,我有一个 VmUserNotificationWithAllRoles 的 ObservableCollection。 我按如下方式填充它,

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

                }

在 UI 中,我使用转换器将 ComboBox 的 itemsSource 绑定到 NotificationOptions。 由于 NotificationOptions 将是列表的 ObservableCollection,我有一个转换器。 这是组合框,

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

问题是组合框没有选择值,它是空白的。 请帮忙。 当我保持断点时,我能够看到 NotificationLevel 的值。

SelectedValuePath仅与SelectedValue结合使用。

例如,如果您有一个项目类,例如

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
}

和一个 ItemsControl 像

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

SelectedItemValue属性设置为例如"Test"将选择Value属性设置为"Test"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM