✅ Allowing a WPF property to be either `string`, `FlowDocument`, or `Block[]`, but that's it
I'm working on a control where I want a property to be flexible. If they pass a
string
, I want to wrap it in a System.Windows.Documents.FlowDocument
and aSystem.Windows.Documents.Paragraph
for them. If they pass one or more blocks, I'll only wrap it in the FlowDocument
. If they just pass a FlowDocument
, I'll just use that. The field won't be editable by the end user.
Can that be done? I don't want to allow just any type to be passed as most wouldn't make sense. In some cases, the array of blocks might be a string with embedded System.Windows.Documents.Span
objects inside.14 Replies
Theres are a few ways to do it, but you kinda need to violate type safety to do so
Ugh.
I know WPF itself seems to do that. Though they seem to have control over the schema and check the object's type. They just let you pass whatever and check it at runtime.
yes, thats what Im suggesting here too
I don't have access to the schema.
It seems your getter type should always be
FlowDocument
here, so I'd recommend making a readonly getter for that, and having either 3 different SetProperty(string/FlowDocument/Block[] value)
setter methods, or having one that takesobject
and throws if its not one of your allowed types
obviously prefer the three setters
oh wait, I just noticied you said "WPF Property"; meaning you want bindings right?
ugh, that makes it... harder.How can the set accessor be of a different type than the get accessor?
it can't for a property
thats why I was suggesting a readonly get accessor, with 3 set methods
but that wont work with bindings
Can't think of a workaround for bindings that doesnt use
public object MyProperty {get...
currently
which obviously sucksI'll probably just go with that and throw exceptions. I hope VS does well with that.
actually, hang on..
you could make your own wrapper type around FlowDocument... with implicit operators for string/flowdocument/block[].. hmm
I wonder if that'd work
thats pretty good
Does it work in XAML?
no idea
give it a shot
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.