如何在XAML中将FormatString作为ConverterParameter传递

时间:2013-10-14 06:44:15

标签: c# wpf xaml

我有转换器绑定。我想将“#,,。0M”格式字符串作为转换器参数传递。

此xaml无效:

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

错误

  

找不到类型'。

如何正确传递此字符串?

3 个答案:

答案 0 :(得分:6)

要么在要传递的字符串上使用单引号:

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

或者使用精心设计的语法进行绑定,如下所示:

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

答案 1 :(得分:1)

其中一种方法是在资源中声明你的字符串并将其传递给你的转换器。

<UserControl.Resources>

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

    </UserControl.Resources>

像下面一样添加

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

可以帮到你

答案 2 :(得分:0)

尝试将字符串保存为资源。

首先添加以下 xmlns 声明

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

然后将字符串保存在资源

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

并按如下方式使用

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