Value converter change back to source

963 Views Asked by At

I am using xml binding with my wpf controls, the XMLDocument is an exposed property of ViewModel. Here is the code:

public class ViewModel : ViewModelBase
{
    private XmlDocument _xmlDataProvider;
    private string _name;
    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
            base.RaisePropertyChangedEvent("Name");
        }
    }
    public XmlDocument XmlDataProvider
    {
        get { return _xmlDataProvider; }
        set
        {
            _xmlDataProvider = value;
            base.RaisePropertyChangedEvent("XmlDataProvider");
        }
    }
}

And my XAML code Looks like this:

<UserControl x:Name="ctrlTemplate" x:Class= "CtrlTemplate"  
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:WPFControl.UI"
             xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
             xmlns:xckt="clr-namespace:Xceed.Wpf.Toolkit;assembly=Xceed.Wpf.Toolkit"
             mc:Ignorable="d" 
             DataContext="{DynamicResource ViewModel}">
    <UserControl.Resources>
            <local:ViewModel x:Key="ViewModel" />
        </ResourceDictionary>
    </UserControl.Resources>

The following control is binded to a node in my xml:

            <DatePicker DataContext="{Binding Path=XmlDataProvider}" SelectedDate="{Binding XPath=dataDocument/loan/paymentDates/paymentDate[1], Converter={StaticResource NullToDateConverter}, UpdateSourceTrigger=PropertyChanged}"/>

My converter in the code segment is as follows:

public class NullToDateConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (String.IsNullOrEmpty(value.ToString()))
        {
            return DateTime.Now.Date;
        }
        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value.ToString();
    }
}

The converter works as intended, if the value of the node is empty. It sets the value of the datetime control to current value.

But I am facing the issue, that if the user for any reason does not change the value of datepicker and tries to save the xml, the value of the node in the xml remains null. What is the best way to do so?

1

There are 1 best solutions below

0
Peter Duniho On

WPF won't update the source if it doesn't think that the target's value is different from the source's. I.e. if the target value doesn't change.

You can, however, force WPF to update the source by calling the BindingExpression.UpdateSource() method. Without a good, minimal, complete code example that reliably reproduces the problem, it's impossible to say specifically how you'd incorporate this into your code. One obvious option would be to call it when the XML is to be saved (i.e. just before).

That might look something like this:

BindingOperations
    .GetBindingExpression(datePicker1, DatePicker.SelectedDateProperty)
    .UpdateSource();

That assumes, of course, that you name your DatePicker control as datePicker1.

This will ensure that whatever the current value of SelectedDate, it is copied back to the original source for the binding (i.e. the path in your XML).