简体   繁体   English

WPF值转换器

[英]WPF Value Converter

I'm making a WPF application and I need a value convertor with more than 1 binding path, is that possible? 我正在制作WPF应用程序,并且我需要一个具有多个绑定路径的值转换器,这可能吗?

It works with 1 path so far: 到目前为止,它只适用于1条路径:

Binding="{Binding Path=Price, Converter={StaticResource vPriceConvertor}}"

I wanna give the Price and the discount to the converter so he can calculate the endprice . 我想给转换器提供Pricediscount ,以便他可以计算出最终endprice

Look at the MultiBinding class. 看一下MultiBinding类。 For example: 例如:

<TextBlock Name="textBlock" DataContext="{StaticResource myViewModel}">
  <TextBlock.Text>
    <MultiBinding Converter="{StaticResource vPriceConvertor}"
                  ConverterParameter="myParameter">
      <Binding Path="Price"/>
      <Binding Path="Discount"/>
      <!-- Insert other paths here -->
    </MultiBinding>
  </TextBlock.Text>
</TextBlock>

Implement IMultiValueConverter instead of IValueConverter to actually do the conversion, since it supports multiple sources. 实现IMul​​tiValueConverter而不是IValueConverter来实际执行转换,因为它支持多个源。

Value converters are intended... for value conversion. 值转换器旨在用于值转换。 That's why they're value converters . 这就是为什么它们是价值转换器

To do what you want, you should make a property EndPrice in view model, and calculate its value in view model. 若要执行所需的操作,应在视图模型中创建属性EndPrice ,并在视图模型中计算其值。 Why are you trying to bring a non-UI logic into UI?? 您为什么要尝试将非UI逻辑引入UI?

Parys - 帕里斯-

You could always create a collection (in View Model) which contains Price and Discount and then pass it through your XAML (through IValueConverter). 您始终可以创建一个包含价格和折扣的集合(在View模型中),然后将其通过XAML(通过IValueConverter)传递。

But to re-iterate @Dennis and everybody else point - then your converter will have calculation logic - which is not recommended. 但是要重申@Dennis和其他所有人的观点-那么您的转换器将具有计算逻辑-不建议这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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