Questions tagged [ivalueconverter]

IValueConverter is an interface defined in Microsoft .NET XAML for use in WPF, Silverlight and Xamarin.Forms. It is used in data binding to convert data from one format to another. The conversion can be one-way or bidirectional.

The .NET Binding classes have an optional Converter property of type IValueConverter which can be used to convert data from its native internal representation to something more convenient to a specific UI objective. A DateTime object could be converted to a string representation in a particular date/time display format, for example, or a numeric value could be converted to a color value for the background of a status control.

IValueConverter defines two methods: Convert and ConvertBack. Implement Convert to convert the source data to the display data representation. Implement ConvertBack to convert the display data representation to the source data representation. This is useful when the UI allows the user to edit the data. In the DateTime example above, if the display data were bound to a TextBox edit control (and the data binding Mode is set to TwoWay), the user could type in or change the date/time values. The IValueConverter specified on the data binding would be responsible for converting the text the user entered into a DateTime value in its ConvertBack method.

For more information, see the MSDN documentation on the IValueConverter interface.

668 questions
138
votes
6 answers

Is there a way to chain multiple value converters in XAML?

I've got a situation in which I need to show an integer value, bound to a property on my data context, after putting it through two separate conversions: Reverse the value within a range (e.g. range is 1 to 100; value in datacontext is 90; user…
Mal Ross
  • 4,551
  • 4
  • 34
  • 46
105
votes
5 answers

how to pass an integer as ConverterParameter?

I am trying to bind to an integer property: and my…
akonsu
  • 28,824
  • 33
  • 119
  • 194
78
votes
4 answers

Moq + Unit Testing - System.Reflection.TargetParameterCountException: Parameter count mismatch

I'm tring to use a lambda with a multiple-params function but Moq throws this exception at runtime when I attempt to call the mock.Object.Convert(value, null, null, null); line. System.Reflection.TargetParameterCountException: Parameter count…
myermian
  • 31,823
  • 24
  • 123
  • 215
60
votes
3 answers

ImageSourceConverter error for Source=null

I'm binding the Source property of an Image to a string. This string may be null in which case I just don't want to display an Image. However, I'm getting the following in my Debug output: System.Windows.Data Error: 23 : Cannot convert ''…
Kai G
  • 3,371
  • 3
  • 26
  • 30
53
votes
7 answers

WPF BooleanToVisibilityConverter that converts to Hidden instead of Collapsed when false?

Is there a way to use the existing WPF BooleanToVisibilityConverter converter but have False values convert to Hidden instead of the default Collapsed, or should I just write my own? I'm on a project where it's tremendous overhead to do something…
Rich
  • 36,270
  • 31
  • 115
  • 154
52
votes
8 answers

Converter With Multiple Parameters

How does one use a converter with Multiple parameters in a Windows Phone 7 Application?
Harshad Bhola
  • 622
  • 1
  • 7
  • 15
51
votes
3 answers

What is the use of ConvertBack method in IValueConverter interface?

What is the use of ConvertBack method in the IValueConverter interface. When will it be called? Or what is the order of invocation of the Convert and ConvertBack methods? I have asked the question here because: I have bound one property of…
Aryan SuryaWansi
  • 2,691
  • 10
  • 31
  • 33
48
votes
2 answers

The point of ValueConversionAttribute class?

What is the point of this attribute? After adding it I still need to make a cast on value object. [ValueConversion(sourceType: typeof(double), targetType: typeof(string))] public class SpeedConverter : IValueConverter { public object…
Lars
  • 1,699
  • 2
  • 22
  • 32
47
votes
6 answers

WPF ValueConverter - Standard return for unconvertible value

Over the course of the last year or so I have seen many different value converters for many different purposes, from many different authors. One thing that sticks out in my mind is the wide variance of the 'default' values that are returned by…
A.R.
  • 15,405
  • 19
  • 77
  • 123
34
votes
3 answers

Why do I get a DependencyProperty.UnsetValue when converting a value in a MultiBinding?

I have an extremely simple IMultiValueConverter that simply OR's two values. In the example below, I want to invert the first value using an equally simple boolean inverter.
Erik Kerber
  • 5,646
  • 7
  • 38
  • 56
22
votes
4 answers

WPF MVVM Radio buttons on ItemsControl

I've bound enums to radio buttons before, and I generally understand how it works. I used the alternate implementation from this question: How to bind RadioButtons to an enum? Instead of enumerations, I'd like to generate a runtime-enumerated set…
Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
22
votes
1 answer

How can you bind to a DynamicResource so you can use a Converter or StringFormat, etc.? (Revision 4)

Note: This is a revision of an earlier design that had the limitation of not being usable in a style, negating its effectiveness quite a bit. However, this new version now works with styles, essentially letting you use it anywhere you can use a…
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
20
votes
3 answers

How to handle exception in Value converter so that custom error message can be displayed

I have a textbox that is bound to a class with a property of type Timespan, and have written a value converter to convert a string into TimeSpan. If a non number is entered into the textbox, I would like a custom error message to be displayed…
sturdytree
  • 849
  • 3
  • 12
  • 26
20
votes
2 answers

Show WPF tooltip on disabled item only

Just wondering if it is possible to show a WPF on a disabled item ONLY (and not when the item is enabled). I would like to give the user a tooltip explaining why an item is currently disabled. I have an IValueConverter to invert the boolean…
dant
  • 575
  • 1
  • 5
  • 11
19
votes
4 answers

Use IValueConverter with DynamicResource?

Is there a way to define a converter when using the DynamicResource extension? Something in the lines of which…
bitbonk
  • 48,890
  • 37
  • 186
  • 278
1
2 3
44 45