简体   繁体   中英

C# XAML Binding of a default with IValueConverter

I'm looking for a reason why my code isn't doing its job: In XAML I use:

<TextBox Text="{Binding Path=Txt_8, Converter={StaticResource DefKonverter}, ConverterParameter='UserAlias'}"/>

In C# there are an IValueConverter which giving me a default value when the ConverterParameter='UserAlias'. For ex. the string 'Jettero'. This works well for that point I see in my TextBox the text 'Jettero'. I saving my record to database, but in the record Txt_8 still NULL ! (Other fields are saved well) Looks like the Binding not updating the record field behind the TextBox.

=========== Update start

CONCLUSION: This is not working because the Binding working in one direction. The Converter good for showing special things what makes your user experince better but not to save it.

=========== Update end

A similar issue backward happens also, In XAML:

<TextBox Text="{Binding Path=Date_1, Converter={StaticResource DefKonverter}, ConverterParameter='\{0:yyyy-MM-dd\}TimeStamp'}"/>

This working as it should in the record behind: when I write in the TextBox the '.' character it 'translates' to the today's date. After I save the record, it contains the date. But in the TextBox I still see the written '.'. In this sit the Binding not updating the TextBox over the record.

=========== Update start

CONCLUSION: This is not working well because the Binding working in one direction. The Converter good for change the data in that shape how you wanna store.

What is still not answered: when I convert '.' into the present date, it is not showing that - now I see the reason. BUT in the Converter if I'm using a Modal window to somehow extend the data what I wrote in (finding a full text for a keyword), that extended information SHOWS UP in my TextBox beside to store of it.

=========== Update end

I don't know what I miss... I checked lot of using of the default and IValueConverter solutions, but this simple sit never came up. Can anyone help?

I think you might be expecting behaviour from a value converter for which it wasn't designed. What is happening is:

On rendering the textbox, the binding reads a value, let's say null , from your property Txt_8, passes that to the converter, which gives it the value to render, in your example 'Jettero'. This means that the visual representation of your null is Jettero . This isn't meant to (and won't) consequently replace your null with 'Jettero' because, according to the binding engine, it has successfully loaded the value from the source and returned it to the target.

The ConvertBack method of the value converter is supposed to cater for the scenario where the value is changed on the UI and needs to be converted back for storage.

Moral of the story: don't use a value converter for specifying a "default" value for the binding. If your property needs a default value, assign it that in your constructor or initializer. If you want your property value to change itself as it is assigned a value, implement it there, instead of in the converter.

For instance, you can define a Date property like this instead of using a converter:

// disclaimer: untested pseudo-code
private DateTime? _dateTimeField;

public string SomeDateProperty
{
   get { return _dateTimeField.ToString('dd-MM-yyyy'); }
   set 
   { 
      if (value == '.') 
        value = DateTime.Today.ToString();

      _dateTimeField = DateTime.Parse(value);
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM