✅ 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
Pobiega
Pobiega9mo ago
Theres are a few ways to do it, but you kinda need to violate type safety to do so
Will Pittenger
Will Pittenger9mo ago
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.
Pobiega
Pobiega9mo ago
yes, thats what Im suggesting here too
Will Pittenger
Will Pittenger9mo ago
I don't have access to the schema.
Pobiega
Pobiega9mo ago
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 takesobjectand 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.
Will Pittenger
Will Pittenger9mo ago
How can the set accessor be of a different type than the get accessor?
Pobiega
Pobiega9mo ago
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 sucks
Will Pittenger
Will Pittenger9mo ago
I'll probably just go with that and throw exceptions. I hope VS does well with that.
Pobiega
Pobiega9mo ago
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
Pobiega
Pobiega9mo ago
No description
Pobiega
Pobiega9mo ago
thats pretty good
Will Pittenger
Will Pittenger9mo ago
Does it work in XAML?
Pobiega
Pobiega9mo ago
no idea give it a shot
public class MyType
{
public MyType(string s) => FlowDocument = new FlowDocument(s);

public MyType(FlowDocument document) => FlowDocument = document;

public MyType(Block[] blocks) => FlowDocument = new FlowDocument(blocks);

public FlowDocument FlowDocument { get; set; }

public static implicit operator MyType(string s) => new(s);
public static implicit operator MyType(Block[] s) => new(s);
public static implicit operator MyType(FlowDocument s) => new(s);
public static implicit operator FlowDocument(MyType t) => t.FlowDocument;
}
public class MyType
{
public MyType(string s) => FlowDocument = new FlowDocument(s);

public MyType(FlowDocument document) => FlowDocument = document;

public MyType(Block[] blocks) => FlowDocument = new FlowDocument(blocks);

public FlowDocument FlowDocument { get; set; }

public static implicit operator MyType(string s) => new(s);
public static implicit operator MyType(Block[] s) => new(s);
public static implicit operator MyType(FlowDocument s) => new(s);
public static implicit operator FlowDocument(MyType t) => t.FlowDocument;
}
Accord
Accord9mo ago
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.
Want results from more Discord servers?
Add your server
More Posts