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
17
votes
1 answer

WPF: One-way IValueConverter

From what I've been told, I need to create an IValueConverter to do my custom formatting. The problem is my formatting is one-way, there is no legitmate ConvertBack implementation. So, how do I handle one-way IValueConverters. (If it helps, this is…
Jonathan Allen
  • 68,373
  • 70
  • 259
  • 447
17
votes
4 answers

Should I declare converters in App.xaml or as a per-file resource?

When declaring converters in a WPF application, should I: Declare all my converters in the App.xaml (i.e. in ) so it's available to the entire application Declare only needed converters for each…
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
16
votes
2 answers

IValueConverter with MarkupExtension

Recently I read about an IValueConverter which also inherits from MarkupExtension. It was something like: internal class BoolToVisibilityConverter : MarkupExtension, IValueConverter { private static BoolToVisibilityConverter converter; …
Tomtom
  • 9,087
  • 7
  • 52
  • 95
15
votes
2 answers

Improved IValueConverter -- MarkupExtension or DependencyObject?

I saw online 2 different approaches to enhancing an IValueConverter. One of them extended a ValueConverter from MarkupExtension, the other from DependencyObject. I can't extend from both, so I'm wondering if any one is better than the other?
michael
  • 14,844
  • 28
  • 89
  • 177
14
votes
4 answers

How can I run code inside a Converter on a separate thread so that the UI does not freeze?

I have a WPF Converter which is slow (computations, online fetching, etc.). How can I convert asynchronously so that my UI doesn't freeze up? I found this, but the solution is to place the converter code in the property -…
tofutim
  • 22,664
  • 20
  • 87
  • 148
13
votes
1 answer

Understanding WPF data binding and value converter interactions

I'm trying to understand what's actually happening behind the scenes on the simplified repro code below. I have a single Window with a ListBox and a TextBlock that are bound together (i.e., master -> detail). I then have a ViewModel with a couple…
GusP
  • 2,454
  • 2
  • 23
  • 32
12
votes
2 answers

Exception: 'IValueConverter' type does not have a public TypeConverter class

I have just run into this exception on an IValueConverter I am implementing: IValueConverter type does not have a public TypeConverter class Has anyone else come across this? What's the cause, and how do I fix? Thanks.
David Veeneman
  • 18,912
  • 32
  • 122
  • 187
12
votes
6 answers

Pass value of a field to Silverlight ConverterParameter

I'm writing my very first Silverlight app. I have a datagrid with a column that has two labels, for the labels, i am using an IValueConverter to conditionally format the data. The label's "Content" is set as such: Content="{Binding HomeScore,…
eidylon
  • 7,068
  • 20
  • 75
  • 118
12
votes
4 answers

How to pass a static value to IValueConverter in XAML

I would like to use static texts fetched from a web service in my WP7 app. Each text has a Name (the indetifier) and a Content property. For example a text could look like this: Name = "M43"; Content = "This is the text to be shown"; I would then…
Zappel
  • 1,612
  • 1
  • 22
  • 37
11
votes
1 answer

IValueConverter with Bound Dependency Properties

I need to determine the StringFormat of some bound TextBlocks at runtime based on the unit system identified in the object to be bound. I Have a converter with a Dependency Property that I would like to Bind to. The Bound value is used in…
Cadair Idris
  • 587
  • 1
  • 9
  • 20
11
votes
1 answer

When does ConvertBack method get called?

I know that when data is about to be displayed, Convert() method is called to convert the data and the converted data is displayed instead. I'm wondering when ConvertBack() method gets called? What is its purpose? I've a DataGrid whose ItemSource…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
10
votes
2 answers

Return a dynamic resource from a converter

I want to change the color of a WPF control depending on the state of a bool, in this case the state of a checkbox. This works fine as long as I'm working with StaticResources: My control
lewi
  • 477
  • 1
  • 5
  • 18
9
votes
2 answers

Binding Double to TextBox

I have often used TextBox to bind to Integers without much problem. However if I try to bind a TextBox to a Double it doesn't work. When I type 5,85 ( , being my cultures decimalSeperator) I pass 585.0 to the double value. How is it being converted…
Ingó Vals
  • 4,788
  • 14
  • 65
  • 113
9
votes
3 answers

Binding ObservableCollection<> to a TextBox

I have data comming back from web service in the form of a ObservableCollection I want to bind the collection to a read-only TextBox so that the user can select and copy the data to the clipboard. To get the collection bound to the Text…
Chris Taylor
  • 52,623
  • 10
  • 78
  • 89
9
votes
2 answers

How and Where to Create StaticResource Key for a Converter within the Simple XAML Window?

I'm having a simple WPF XAML Window, I need to Create a StaticResource Key with in the following XAML. The XAML Source Code is
B.Balamanigandan
  • 4,713
  • 11
  • 68
  • 130
1
2
3
44 45