简体   繁体   中英

How to pass a FormatString as ConverterParameter in XAML

I have a binding with converter. I want to pass the "#,,.0M" format string as converter parameter.

This xaml in not valid:

<local:SalesPerformanceControl FirstSalesVolume="{Binding Path=TodaySalesVolume, Converter={StaticResource ResourceKey=decimalToFormatedStringConverter}, ConverterParameter=#,,.0M}"/>

Error :

The type '' was not found.

How to pass this string correctly?

Either use single quotes on the string to be passed:

       <local:SalesPerformanceControl FirstSalesVolume="{Binding Path=TodaySalesVolume, Converter={StaticResource ResourceKey=decimalToFormatedStringConverter}, ConverterParameter='#,,.0M'}"/>

OR use elaborate syntax to bind like below:

    <local:SalesPerformanceControl>
        <local:SalesPerformanceControl.FirstSalesVolume>
            <Binding Path="TodaySalesVolume" Converter="{StaticResource decimalToFormatedStringConverter}" ConverterParameter="#,,.0M" />
        </local:SalesPerformanceControl.FirstSalesVolume>
    </local:SalesPerformanceControl>

One of the way can be declare your string in resources and pass it to your converter.

<UserControl.Resources>

 <sys:String x:Name="strParam">#,,.0M</sys:String>

    </UserControl.Resources>

Add like as bellow

<local:SalesPerformanceControl FirstSalesVolume="{Binding Path=TodaySalesVolume, Converter={StaticResource ResourceKey=decimalToFormatedStringConverter},   ConverterParameter={StaticResource strParam}}"/>

may help you

Try saving the string as a resource.

First add the following xmlns declaration

xmlns:sys="clr-namespace:System;assembly=mscorlib"

Then save the string in the resources

<sys:String x:Key="format">#,,.0M</sys:String>

And use it as follows

<local:SalesPerformanceControl FirstSalesVolume="{Binding Path=TodaySalesVolume, Converter={StaticResource ResourceKey=decimalToFormatedStringConverter}, ConverterParameter={StaticResource ResourceKey=format}}"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM