Reputation: 905
In a C# project, I have a complex model using lots of classes nested in lists and dictionaries (e.g. object A
has a list of instances of B
, which have a dictionary where values are instances of C
).
In one of my pages, this model is shown in a complex view using nested ItemsControl
s.
In addition, there is a Settings
class which stores user preferences, some of which are bound to checkboxes on the page.
Now, I want to bind properties of some controls within the DataTemplate
s to a combination of a model property and a setting. For example, let's say that C
has a property IsBoring
, and there is a settings Settings.HideBoringStuff
. I want to bind the visibility of the TextBlock
representing C
to the obvious combination of these properties.
I have no idea how to do that without ugly hacks. Here are a few of my ideas and why they don't work:
Use a MultiBinding
, which is specifically designed to do this. However, MultiBinding
is not available in UWP projects.
Bind to multiple properties on the page that implement the logic in their getters and setters. This doesn't work because I'm inside a DataTemplate
, so I need multiple independent copies of this logic.
Use a Converter
to convert the model property, passing in the setting as a ConverterProperty
. However, the ConverterProperty
is no DependencyProperty
and therefore cannot be bound.
Build the needed properties right into the model – Settings
is a singleton anyway. This feels really ugly, as I'd be mixing unnecessary dependencies and view logic into my model.
Build separate classes that wrap the model classes, also store the Settings
object to use, and then offer the combined properties. This feels really ugly too, as I'd need to replicate the whole object hierarchy of the model. (In the example, ViewA
needs to offer a list of ViewB
s, each of which have a dictionary where the values are the corresponding ViewC
s.)
Wait for Microsoft to bring MultiBinding
back. Unluckily, I'm lacking the required optimism.
Which are clean ways to do this in a UWP application?
Upvotes: 5
Views: 2319
Reputation: 6142
True that Multibinding hasn't been around on the new win dev stacks. BUT Cimbalino toolkit has had it since early wp8 days. It also has an UWP port.
So maybe try that!
Blogpost from the early days explaining the use: https://www.pedrolamas.com/2013/05/17/cimbalino-windows-phone-toolkit-multibindingbehavior/ Getting hold of Cimbalino is done through nuget and is available on Github here https://github.com/Cimbalino/Cimbalino-Toolkit
Upvotes: 4