简体   繁体   English

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

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

I have a class as follows,我有一个类如下,

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

Now I have an other class as follows which implements the above class,现在我有另一个类,如下所示,它实现了上述类,

 public class VmUserNotificationWithAllRoles : VmUserNotification
        {
        }

In the ViewModel I have a ObservableCollection of VmUserNotificationWithAllRoles.在 ViewModel 中,我有一个 VmUserNotificationWithAllRoles 的 ObservableCollection。 I am populating it as follows,我按如下方式填充它,

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

                }

In the UI I am binding the itemsSource of the ComboBox to NotificationOptions with a Converter.在 UI 中,我使用转换器将 ComboBox 的 itemsSource 绑定到 NotificationOptions。 Since that NotificationOptions will be a ObservableCollection of list, I have a converter.由于 NotificationOptions 将是列表的 ObservableCollection,我有一个转换器。 Here is the combobox,这是组合框,

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

Here is my converter,这是我的转换器,

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

Problem is the Combobox does not select the value, its blank.问题是组合框没有选择值,它是空白的。 Please help.请帮忙。 WHen I keep a breakpoint I am able to see a value for NotificationLevel.当我保持断点时,我能够看到 NotificationLevel 的值。

SelectedValuePath only works in conjunction with SelectedValue . SelectedValuePath仅与SelectedValue结合使用。

If you have for example an item class like例如,如果您有一个项目类,例如

public class Item
{
    string Value { get; set; }
}

with a view model like使用像这样的视图模型

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

    public string SelectedItemValue { get; set; } // must fire PropertyChanged
}

and an ItemsControl like和一个 ItemsControl 像

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

setting the SelectedItemValue property to eg "Test" would select the Item with a Value property set to "Test" .SelectedItemValue属性设置为例如"Test"将选择Value属性设置为"Test"

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

相关问题 ComboBox SelectedValuePath验证不起作用 - ComboBox SelectedValuePath Validation not working WPF组合框与集合displayvaluepath绑定,SelectedValuePath不起作用 - WPF combobox bind with collections displayvaluepath,SelectedValuePath not working 将ListView绑定到具有Converter的ICollectionView时,IsSynchronizedWithCurrentItem不起作用 - IsSynchronizedWithCurrentItem not working when ListView is bound to an ICollectionView with a Converter 在组合框中使用转换器绑定项目 - Use converter on bound items in combobox DataGrid中的ComboBox:ItemSource绑定到动态资源,如何设置默认行? - ComboBox in DataGrid: ItemSource bound to dynamic resource, how to set default row? WPF:Combobox在绑定ItemSource集合更改后丢失选定的索引 - WPF: Combobox losing selected index after bound ItemSource collection changes WPF如何在具有不同项目源的列表视图中设置绑定组合框的selecteditem - WPF How to Set selecteditem of bound combobox in a listview with different itemsource 隐藏代码中的 ComboBox SelectedValuePath 问题 - Issue with ComboBox SelectedValuePath in code behind WPF 组合框绑定和 SelectedValue 与 SelectedValuePath - WPF Combobox binding and SelectedValue with SelectedValuePath 绑定列表时 XAML 标记错误<string>到 WPF 中的 ComboBox ItemSource - XAML markup error when binding List<string> to a ComboBox ItemSource in a WPF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM