Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF Data Binding and IValueConverter
    text
    copied!<p>Why is it that when I use a converter in my binding expression in WPF, the value is not updated when the data is updated.</p> <p>I have a simple Person data model:</p> <pre><code>class Person : INotifyPropertyChanged { public string FirstName { get; set; } public string LastName { get; set; } } </code></pre> <p>My binding expression looks like this:</p> <pre><code>&lt;TextBlock Text="{Binding Converter={StaticResource personNameConverter}" /&gt; </code></pre> <p>My converter looks like this:</p> <pre><code>class PersonNameConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { Person p = value as Person; return p.FirstName + " " + p.LastName; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } </code></pre> <p>If I bind the data without a converter it works great:</p> <pre><code>&lt;TextBlock Text="{Binding Path=FirstName}" /&gt; &lt;TextBlock Text="{Binding Path=LastName}" /&gt; </code></pre> <p>What am I missing?</p> <p>EDIT: Just to clarify a few things, both Joel and Alan are correct regarding the INotifyPropertyChanged interface that needs to be implemented. In reality I do actually implement it but it still doesn't work.</p> <p>I can't use multiple TextBlock elements because I'm trying to bind the Window Title to the full name, and the Window Title does not take a template.</p> <p>Finally, it is an option to add a compound property "FullName" and bind to it, but I'm still wondering why updating does not happen when the binding uses a converter. Even when I put a break point in the converter code, the debugger just doesn't get there when an update is done to the underlying data :-(</p> <p>Thanks, Uri</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload