ComboBox mvvm中的静态值绑定

时间:2017-02-22 08:35:55

标签: c# wpf xaml mvvm combobox

我有一个组合框,其值为[天,周,月,年]

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

除了comboBox,我有一个文本框。如果用户从comboBox中选择“周”并将值2放在文本框中,则在数据库中我必须保存2 * 7 = 14 [天]。

要执行此功能,我已创建了期间集合。

 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...

}

现在在我的viewModel中我试图实现一个返回天数的方法。

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]
}

但我很困惑 -

同时保存我将如何与文本框值合并以及组合框天数的返回值[如果在文本框中输入2并在comobox中选择一周,我需要值14]

请在wpf mvvm概念中帮助我。我在网上试过,但是在mvvm中找不到任何内容。

1 个答案:

答案 0 :(得分:1)

在您的视图模型中,您需要添加另一个将与文本框绑定的属性,以便您可以在视图模型中访问 TextBox 的值,因此首先添加视图模型中的新属性:

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]
}

和xaml sepcify TextBox与属性的绑定:

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

现在在GetPeriod方法中,您既可以拥有价值又可以做任何商业要求:

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

    var value = _providedNumber * selectedDuration.NumberOfDays;
}