Click here to Skip to main content
15,867,330 members
Articles / Desktop Programming / WPF
Tip/Trick

WPF Display Value Converter

Rate me:
Please Sign up or sign in to vote.
4.38/5 (8 votes)
23 Jun 2016CPOL1 min read 12.1K   105   6   5
This presents a simple converter that supports binding to an object, but display of a property in that class.

Introduction

This simple IValueConverter allows binding to an object, then the actual binding is to a property or ToString() value in that class, specifying the name of property in the ConverterParameter.

Background

I was using IDataErrorInfo for a form, and some of the data in the ViewModel were objects. This form was a read-only form and I wanted to show any data issues. There was also an edit form where the values could be changed. I could have done some work to get the IDataErrorInfo to work, but it seemed like a IValueConverter was a better option since it could be used in many cases, and was actually very simple and easy to maintain.

The Converter

The IValueConverter handles OneWay Binding only since converters do not provide an interface to the class that would be updated for the ConvertBack method. It could be changed so that it would just return the new value and the property set accessor could update the value.

The Convert method handles two cases: where there is a value provided in the ConvertParameter and when there is not. If a ConvertParameter is not provided, then the ToString() method of the object is returned. If a ConvertParameter is provided, then reflection is used to get the value of the property with the name contained in the ConvertParameter:

C#
internal class DisplayValueConverter : MarkupExtension, IValueConverter
{
 public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
 {
  if (string.IsNullOrWhiteSpace(parameter?.ToString()))
  {
   return value?.ToString() ?? value;
  }
  else
  {
   if (value == null) return DependencyProperty.UnsetValue;
   var property = value.GetType().GetProperty(parameter.ToString());
   Debug.Assert(property != null,
        $"Could not find property {property} in class {value.GetType().Name}");
   return property.GetValue(value);
  }
 }

 public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
 {
  throw new NotImplementedException();
 }

 public override object ProvideValue(IServiceProvider serviceProvider) { return this; }
}

The Sample

History

  • 16/06/23: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Clifford Nelson Consulting
United States United States
Has been working as a C# developer on contract for the last several years, including 3 years at Microsoft. Previously worked with Visual Basic and Microsoft Access VBA, and have developed code for Word, Excel and Outlook. Started working with WPF in 2007 when part of the Microsoft WPF team. For the last eight years has been working primarily as a senior WPF/C# and Silverlight/C# developer. Currently working as WPF developer with BioNano Genomics in San Diego, CA redesigning their UI for their camera system. he can be reached at qck1@hotmail.com.

Comments and Discussions

 
QuestionNot picking on you or anything :)... Pin
SledgeHammer0123-Jun-16 9:07
SledgeHammer0123-Jun-16 9:07 
AnswerRe: Not picking on you or anything :)... Pin
Clifford Nelson23-Jun-16 9:55
Clifford Nelson23-Jun-16 9:55 
I know. As I stated in the article, the purpose was to continue to let IDataErrorInfo work on the base class. This will let you put the [Required] on the property in the base class. Then when the base class is null, IDataErrorInfo still works, so get the error indicator, and yet can still display a property in the child class. In my case in another window I have edit capability, but not in another window. I do extended paths all the time.
AnswerRe: Not picking on you or anything :)... Pin
Clifford Nelson23-Jun-16 10:35
Clifford Nelson23-Jun-16 10:35 
GeneralRe: Not picking on you or anything :)... Pin
SledgeHammer0123-Jun-16 11:54
SledgeHammer0123-Jun-16 11:54 
AnswerRe: Not picking on you or anything :)... Pin
Clifford Nelson23-Jun-16 12:24
Clifford Nelson23-Jun-16 12:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.