Can't easily Map collection from .NET Standard to MetadataControl?
Describe the bug
Since we've been doing a lot more with the MVVM Toolkit lately, I was thinking about the scenario where your data model may be coming from .NET Standard. E.g. having a list of items for tags or something.
Currently, you'd have to hard create a new list of MetadataItem from that list and make sure you observe any changes to re-map yourself.
It'd be nice if we could take in any collection of items and map to the properties we need instead. Like ComboBox does with DisplayMemberPath for instance. Though this may be a bit odd for commands?
It's also a lot more reflection heavy to do it that way... And I don't think we'd want a general MetadataItem type class in our .NET Standard libraries to be tied specifically to a control in our UI library...
Do we see this as an issue with using the control?
Environment
NuGet Package(s): 7.1.0-rc1
Package Version(s):
Windows 10 Build Number:
- [ ] Fall Creators Update (16299)
- [ ] April 2018 Update (17134)
- [ ] October 2018 Update (17763)
- [ ] May 2019 Update (18362)
- [ ] May 2020 Update (19041)
- [ ] Insider Build ({build_number})
App min and target version:
- [ ] Fall Creators Update (16299)
- [ ] April 2018 Update (17134)
- [ ] October 2018 Update (17763)
- [ ] May 2019 Update (18362)
- [ ] May 2020 Update (19041)
- [ ] Insider Build ({build_number})
Device form factor:
- [ ] Desktop
- [ ] Xbox
- [ ] Surface Hub
- [ ] IoT
Visual Studio version:
- [ ] 2017 (15.{minor_version})
- [ ] 2019 (16.{minor_version})
- [ ] 2022 (17.{minor_version})
Additional context
FYI @vgromfeld @Sergio0694
Hello michael-hawker, thank you for opening an issue with us!
I have automatically added a "needs triage" label to help get things started. Our team will analyze and investigate the issue, and escalate it to the relevant team if possible. Other community members may also look into the issue and provide feedback 🙌
Agreed. We are actually using the following converter function in our code when binding our .Net standard VM to the UI. This allows us to only expose standard types from the VM layer and have them converted to MetadataUnit. ICommandWithLabel is a custom interface which adds a label to an ICommand.
This could be something we add in the toolkit as a basic helper for the MetadataControl
public static IEnumerable<MetadataUnit> GetMetadataUnits(IEnumerable<object> source)
=> source.Select(
item =>
{
var unit = default(MetadataUnit);
switch (item)
{
case string label:
unit.Label = label;
break;
case ICommandWithLabel commandWithLabel:
unit.Label = commandWithLabel.Label;
unit.Command = commandWithLabel.Command;
break;
default:
unit.Label = item.ToString();
break;
}
return unit;
});