Ludo237
Ludo237

Reputation: 1727

Bind string[] to XAML using TwoWay

I've a Model and I receive a property called birth in this format

birthday, birthplace

I've set this inside the Model

private string _birth;
/// <summary>
/// Birth information in format: birthday, birthplace
/// </summary>
[Column("birth")]
[JsonProperty("birth")]
public string Birth
{
    get { return this._birth; }
    set
    {
        this._birth= value;
        OnPropertyChanged();
        OnPropertyChanged("BirthData");
    }
}

Also I've set a public modifier like this

    /// <summary>
    /// Split the birth data if exists
    /// </summary>
    public string[] BirthData
    {
        get { return this.Birth?.Split(',');  }
    }

In my XAML file I've setup everything correctly for my Model, except that I can't understand how I can use TwoWay binding on something like this. Below you will find the extract of the XAML for this particular property

<Label x:Name="BirthdayLabel" Content="Nato Il" />
<DatePicker x:Name="BirthdayDateTimePicker" SelectedDate="{Binding selectedModel.BirthData[0], Mode=TwoWay}"></DatePicker>
<Label x:Name="BirthplaceLabel" Content="Nato A"/>
<TextBox x:Name="BirthplaceTextBox" Text="{Binding selectedModel.BirthData[1], Mode=TwoWay}"/>

Of course this is not working properly because I'll ended up with selectedModel using the old information about the property even though the binding works as expected, I think that the TwoWay binding doesn't work with an Array of data like that.

I cannot change the DataSource and I've to find a method to use one textbox and one date picker and resemble together inside my ViewModel, which doesn't have much except a single method called Update() which take the current selectedModel.

Upvotes: 1

Views: 285

Answers (1)

Emmanuel DURIN
Emmanuel DURIN

Reputation: 4913

ViewModel is about preparing the data for the View

So I suggest that you split and parse the data in the View Model and expose two properties for BirthDate and BirthPlace :

class Person
{
    private string _birth;
    public string Birth{
        get { return this._birth; }
        set
        {
            this._birth = value;
            SplitBirthIntoBirthDayAndBirthPlace();
            OnPropertyChanged();
        }
    }
    private DateTime _birthday;
    public DateTime Birthday{
        get { return _birthday; }
        set
        {
            _birthday = value;
            ReComputeBirth();
            OnPropertyChanged();
        }
    }
    // Same for Birthplace ...
    private void ReComputeBirth(){
        // ... Format the data as expected ...
        _birth = _birthday + ", " + _birthplace;
    }
    private void SplitBirthIntoBirthDayAndBirthPlace()
    {
        String[] values = _birth.Split(',', ' ');
        // ... really make the parse here to fill _birthplace and _birthdate...
    }
    // ....
}

And the binding is simpler:

    <DatePicker x:Name="BirthdayDateTimePicker"
                SelectedDate="{Binding selectedModel.Birthday, Mode=TwoWay}"/>

    <TextBox x:Name="BirthplaceTextBox"
             Text="{Binding selectedModel.Birthplace, Mode=TwoWay}"/>

Upvotes: 2

Related Questions