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. 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. Since that NotificationOptions will be a ObservableCollection of list, I have a converter. 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.
SelectedValuePath
only works in conjunction with 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 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"
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.