1

Given the following:

public partial class MyCustomControl : UserControl, IValueConverter

How would I go about setting the XAML to access the already (Self) created instance of the control to do the conversion?

<TextBlock Text="{Binding Converter={ ??? }}" />

The reason I am doing this is because I have a DP on the control that tells me what property of the object I need to reflect through to display it's value in the text of that TextBlock. So... if I implement IValueConverter in the codebehind, and tell it to use that as the converter, as long as it's using the same instance, I should be able to get everything I need.

Thanks!

7
  • 4
    I would definitely recommend changing your logic into it's own type, simply because it's a separation of concerns then. Your user control shouldn't have to be also a TypeConverter; it's a different logical thing. It also becomes trivially easy to use your TypeConverter then.
    – Tejs
    Commented Sep 6, 2011 at 18:58
  • Did you try to use (self) keyword?
    – Tigran
    Commented Sep 6, 2011 at 19:04
  • The problem with that is getting the property to reflect out at that point. If I have a seperate converter, the only thing I'll see is the object that's bound to the item. Considering this is a usercontrol and the property I need to reflect out is unknown until the user of the control sets it, I need a way to get that object as well as the property to reflect out.
    – Mike
    Commented Sep 6, 2011 at 19:04
  • 1
    Yes I tried self, didn't work.
    – Mike
    Commented Sep 6, 2011 at 19:05
  • Did you try to declare MyCustomControl like a static resource and statically link it to Converter?
    – Tigran
    Commented Sep 6, 2011 at 19:07

1 Answer 1

1

Bad idea, create a separate converter. If you need more information in it just add properties to it (e.g. one for the whole UserControl)

Not the answer you're looking for? Browse other questions tagged or ask your own question.