Strange
Strange

Reputation: 166

ComboBox Static value binding in mvvm

I have one comboBox with value [ days,weeks,months,years]

   <ComboBox Width="130" Margin="0,10,0,16" ItemsSource="{Binding PeriodCollection }" 
               SelectedItem="{Binding SelectedDuration }" 
               Text=""  DisplayMemberPath="Name" />

Beside comboBox , I have one Textbox. If user selects "weeks" from comboBox and put value 2 in textbox, in database I have to save 2*7 = 14 [ days].

To do this functionality I have created period collection.

 public class Period : BaseObject
    {
        private string name;
        public string Name 
        { 
            get { return name; } 
            set { name = value; NotifyPropertyChanged(); } 
        }

        private int numberOfDays;
        /// <summary>
        /// number of days in period
        /// </summary>
        public int NumberOfDays 
        { 
            get { return numberOfDays; } 
            set { numberOfDays = value; NotifyPropertyChanged(); } 
        }        

        public Period(string perdioName, int numberOfDayInPeriod)
        {
            Name = perdioName;
            NumberOfDays = numberOfDayInPeriod;
        }
    }
}
 public static class PeriodManager
{
    private static ObservableImmutableList<Period> periodCollection;
    /// <summary>
    /// return a collection of time period in days        /// 
    /// </summary>
    /// <returns>Days, Week,Month, year</returns>
    public static ObservableImmutableList<Period> GetPeriodCollection()
    {
        if (periodCollection == null)
        {
            periodCollection = new ObservableImmutable.ObservableImmutableList<Entities.Period>();
            periodCollection.Add(new Period("Days", 1));
            periodCollection.Add(new Period("Week", 7));
            periodCollection.Add(new Period("Month", 30));
            periodCollection.Add(new Period("Year", 365));
        }
        return periodCollection;
    }

    /// you can create method can return a period from a day number...

}

Now in my viewModel I am trying to implement a method which will return days.

private ObservableImmutableList<Period> periodCollection;
public ObservableImmutableList<Period> PeriodCollection { get { return periodCollection; } set { periodCollection = value; NotifyPropertyChanged(); } }

private Period selectedDuration;
public Period SelectedDuration { get { return selectedDuration; } set { selectedDuration = value; NotifyPropertyChanged(); } }

private void GetPeriod()
{
    PeriodCollection =  PeriodManager.GetPeriodCollection();
    //here I need to write logic to merge from both values [ txtBox+combox]
}

But I am confused with -

while saving how I'll merge with textbox value with returned value of combobox days [ if entered 2 in textbox and selected week in comobox, i need value 14]

Please help me with this in wpf mvvm concept. I tried in web but was not able to find any in mvvm.

Upvotes: 1

Views: 959

Answers (1)

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

In your View Model you need to add another property that will be bind with the Text Box so that you can have access to value of TextBox as well in your View Model, so first of all add a new property in your View Model :

private ObservableImmutableList<Period> periodCollection;
public ObservableImmutableList<Period> PeriodCollection 
{ 
   get { return periodCollection; } 
   set { periodCollection = value; NotifyPropertyChanged(); } 
}

private Period selectedDuration;
public Period SelectedDuration 
{ 
  get { return selectedDuration; } 
  set { selectedDuration = value; NotifyPropertyChanged(); } 
}

private int _providedNumber;
public int ProvidedNumber
{ 
  get { return _providedNumber; } 
  set { _providedNumber= value; NotifyPropertyChanged(); } 
}

private void GetPeriod()
{
    PeriodCollection =  PeriodManager.GetPeriodCollection();
    //here I need to write logic to merge from both values [ txtBox+combox]
}

and in xaml sepcify binding of TextBox with the property:

<TextBox Text="{Binding Path=ProvidedNumber}"></TextBox>

Now in GetPeriod method you can have both value and do whatever is business reuqirements :

private void GetPeriod()
{
    PeriodCollection =  PeriodManager.GetPeriodCollection();

    var value = _providedNumber * selectedDuration.NumberOfDays;
} 

Upvotes: 1

Related Questions