Parys Bernard
Parys Bernard

Reputation: 159

WPF Value Converter

I'm making a WPF application and I need a value convertor with more than 1 binding path, is that possible?

It works with 1 path so far:

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

I wanna give the Price and the discount to the converter so he can calculate the endprice.

Upvotes: 1

Views: 939

Answers (3)

Patrick
Patrick

Reputation: 884

Parys -

You could always create a collection (in View Model) which contains Price and Discount and then pass it through your XAML (through IValueConverter).

But to re-iterate @Dennis and everybody else point - then your converter will have calculation logic - which is not recommended.

Upvotes: 1

Dennis
Dennis

Reputation: 37770

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. Why are you trying to bring a non-UI logic into UI??

Upvotes: 5

akton
akton

Reputation: 14386

Look at the MultiBinding class. 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.

Upvotes: 5

Related Questions