Note that there are some explanatory texts on larger screens.

plurals
  1. POXAML Automatic Type Conversion
    primarykey
    data
    text
    <p>I've noticed it's possible to return a string like "Visible", "Collapsed", "Images/xyz.png" or "#FFB3D1" from a value converter and the magic of bindings manages to figure it out. Hide/Show the UI Element, find the xyz.png image or colour something pink...</p> <p>I've taken this for granted for a long time, it now it doesn't work with my latest code, So my Question is how can I manually call this functionality?</p> <hr /> <p><strong>Explanation:</strong></p> <p>I've scaled up by creating a custom MarkupExtension, which attaches a MultiConverter attaches it to a MultiBinding and returns the initialised binding. However when this multi converter returns strings like "#FFB3D1" or "Red", nothing seems to happen.</p> <pre><code>// PseudoCode from my MarkupExtension, setting up &amp; returning the binding/multi-converter public override object ProvideValue( IServiceProvider serviceProvider ) { MultiBinding outputBinding = new MultiBinding(); foreach ( Binding b in bindings ) { outputBinding.Bindings.Add( b ); } outputBinding.Converter = converter; return outputBinding.ProvideValue( serviceProvider ); } </code></pre> <p>I presume that because I'm creating the Multibinding + Converter in code, it's skipping a step somewhere in the <code>Binding.Magic</code> namespace.</p> <hr /> <p><strong>Solution:</strong></p> <pre><code> public override object ProvideValue( IServiceProvider serviceProvider ) { // get targets IProvideValueTarget serv = (IProvideValueTarget)serviceProvider.GetService( typeof( IProvideValueTarget ) ); // get Type Converter object[] typeConverterAttributes = ( (DependencyProperty)serv.TargetProperty ).PropertyType.GetCustomAttributes( typeof( TypeConverterAttribute ), true ); TypeConverter typeConverter = null; if ( typeConverterAttributes.Length &gt; 0 ) { TypeConverterAttribute attr = (TypeConverterAttribute)typeConverterAttributes[0]; typeConverter = (TypeConverter)Activator.CreateInstance( Type.GetType( attr.ConverterTypeName ), false ); } </code></pre> <p>It is then simply a case of applying the Type Converter manually</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload