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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19354763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:50:39  来源:igfitidea点击:

How to pass a FormatString as ConverterParameter in XAML

c#wpfxaml

提问by Eugene Maksimov

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

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

This xaml in not valid:

此 xaml 无效:

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

Error:

错误

The type '' was not found.

未找到类型 ''。

How to pass this string correctly?

如何正确传递这个字符串?

回答by Nitin

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>

回答by Debashrita

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

可能会帮助你

回答by Omri Btian

Try saving the string as a resource.

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

First add the following xmlnsdeclaration

首先添加以下xmlns声明

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}}"/>