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