views:

46

answers:

2

So I've gotten in the habit of using WPF/C# value converters because they are awesome. I usually just have a folder set up for value converters and then access them in whatever xaml files I might need them.

I'm currently developing a user control that will have some value converters that I do not want other classes to be able to access. In other words, value converter exists in the code behind file and should be accessible only from that file and the associated xaml. My first thought was to throw it inside the code behind file as a nested class, but I can't find a way to access the nested class from within the xaml file. Am I going in the right direction or is there something else I should be doing?

I could go the cheap way and just throw this control into its own namespace, but that doesn't really solve my problem.

Any help or guidance is appreciated. Thanks!

Below is roughly what I want:

public partial class MyControl: UserControl 
{ 
     //misc code for control 
     private class MyValueConverter : IMultiValueConverter 
     {
      //conversion functions 
     }
}

is what I have in mind.

Normally, value converters are accessed from WPF like:

`<local:MyValueConverter x:Key="MyValueConverter" />`

This only works if they are in the same namespace. I cannot get this to work if it is a nested class. I've met my goal of making the valueconverter visible only to this user control, but I cannot figure out for the life of me how to access it from within the xaml.

My problem is accessing this converter from in the xaml.

+1  A: 

In your code-behind file, mark the methods that contain your converter code as private just like you would any other methods or properties that you wouldn't want to be visible outside of the class. Remember, the XAML and the code-behind file are (by default) both partial representations of the same class, so anything you declare in your code-behind file, even if private, is still visible to the corresponding XAML file.

Ben McCormack
I'm assuming I would still keep it as a nested class? I'm used to declaring the converters in the xaml resource dictionary like: <local:convertername x:Key="convertername" /> where "local" is whatever namespace the converter is in. I'm guessing this will be slightly different?
brandon
@brandon Ah. I thought you were declaring your converters in your code-behind when you said "value converter exists in the code behind file...". If you are declaring it in XAML, I'm not sure exactly how you would do it. You can check the generated code from the XAML and see the protection level of the converters as it exists there. It may already be making it `private` or `protected`.
Ben McCormack
Sorry for the ambiguity, I'm edditing the original post to clarify.
brandon
@brandon oh I see now. Since the conversion functions are within `MyValueConverter` and that is marked `private`, that code is only visible from within that particular code-behind file and the corresponding XAML file (and possible other partial classes if you've created them). Even though you are consuming that code in your XAML, it still should not be visible to other classes.
Ben McCormack
A: 

It is not possible (look here for the reason - 2nd paragraph about requirements)

Your custom class must not be a nested class. Nested classes and the "dot" in their general CLR usage syntax interfere with other WPF and/or XAML features such as attached properties.

You can of course work around that - but that limits you to setting the valueconverter in the code-behind (which is yucky :-))

I'm curious, though, why would you not want other classes to use it?

Goblin