0

I've got a WPF IMultiValueConverter that I am forced to make stateful (for ConvertBack functionality). This requires me to mark all uses of it as x:Shared="False" in XAML. I'm not happy with this approach but I for my particular problem, I have yet to find a better alternative.

In the interests of aiding future developers on this codebase, I would like to write some checking code-behind that will at at least dump a debug warning message to them they have used the converter but failed to mark it as x:Shared="False".

Is there way for me, in code-behind of my converter, to retrieve the value of "x:Shared" attribute that someone has marked in XAML?

Edit to clarify: I want to be sure that when the user declares an instance of my converter in the resource section, that they do this:

<core:LengthToValueConverter x:Key="MyConverter" x:Shared="False"/>

and not just this:

<core:LengthToValueConverter x:Key="MyConverter"/>

(In case anyone wants to know why I'm doing this, I'm performing unit conversion much as described in this thread and am facing the same ConvertBack issue that the poster did. I welcome better alterative approaches. )

Joe
  • 5,394
  • 3
  • 23
  • 54
  • Have you tried data binding? – Sam Xia Dec 25 '19 at 18:44
  • Hi Sam. I am not sure I understand your question. I obviously must use databinding when using the converter. (Where else would it get its inputs for IMultiValueConverter.Convert?) Unfortunately in ConvertBack, I don't get to see all of the inputs that were passed to Convert. Therefore I must statefully store them within the converter during the call to Convert. This means it must be marked as not shared – Joe Dec 25 '19 at 18:46
  • 1
    You can try MultiBinding, I will write an answer for you – Sam Xia Dec 25 '19 at 18:49
  • 1
    I do not think you can achieve this. It is like you cannot get instance name in class definition. – Sam Xia Dec 25 '19 at 19:44
  • Thank you. I figured it was like that. I was just hoping I was wrong – Joe Dec 25 '19 at 20:16
  • 2
    Make your converter a markup extension as well. Then each will be a separate thing by default and they won't need to be forced to apply x:shared false. http://www.thejoyofcode.com/WPF_Quick_Tip_Converters_as_MarkupExtensions.aspx – Andy Dec 26 '19 at 12:10
  • Actually that is sorry of a standard practice for me. Full disclosure is that is what I am currently doing. Unfortunately, merely making a control a markup extension does not prevent one from declaring it as a resource and then using StaticResource to employ it;. They also have to be sure to declare an instance inline each time they use it. In the end, I guess I am going to have to just comment the hell out of this and hope future developers read the comments – Joe Dec 26 '19 at 14:24
  • I'm wondering if the person who downvoted this question might explain why they did. I'm not complaining or asking you to change your vote or anything but I'm trying to understand what was wrong with the question so I can be better in the future. Was there some obvious answer I should have searched for? Did I ask the question badly? – Joe Dec 26 '19 at 20:07

0 Answers0