1

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.

Strange
  • 166
  • 1
  • 10
  • 1
    You don't need to add `ComboBoxItem`s in xaml if you are binding to `ItemsSource`. In MVVM you would normally have all collections defined in ViewModel/Model and view simply bind to them. – Sinatr Feb 22 '17 at 08:39
  • itemsource code is blank right now with "_Collection" I need complete solution, what logic is best for this. – Strange Feb 22 '17 at 08:42
  • 2
    Possible duplicate of [WPF ComboBox binding ItemsSource](http://stackoverflow.com/questions/28373643/wpf-combobox-binding-itemssource). See accepted answer for an example of `ComboBox.ItemsSource` binding. Or maybe you want to [bind to static property](http://stackoverflow.com/q/936304/1997232). – Sinatr Feb 22 '17 at 08:43
  • itemsource edited, check. it was never my problem. problem is with merging both values and saving. – Strange Feb 22 '17 at 08:49
  • which is the ViewModel of your View right now? – Ehsan Sajjad Feb 22 '17 at 08:52
  • 1
    You can bind `TextBox.Text` to `double Number` property and `ComboBox.SelectedItem` to `Period SelectedPeriod` property. Then you need a command (or simply call some method from setters of those properties): in this command do `Number * SelectedPeriod.NumberOfDays`. – Sinatr Feb 22 '17 at 08:58
  • @ehsan viewmodel code added..... check – Strange Feb 22 '17 at 09:00
  • @sintar ok. I'll try this. – Strange Feb 22 '17 at 09:01
  • Side note... what's an ObservableImmutableList, and exactly what's the point of its existence? If it's immutable, why would it have any change notification??? I feel dizzy. Someone, bring me an alcohol. –  Feb 22 '17 at 18:46
  • it is our own defined class library in our project. – Strange Feb 23 '17 at 05:06

1 Answers1

1

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;
} 
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160