2014-12-19 80 views
0

我有一個組合框,當用戶從存儲在ViewModelMain類的變量中的數據中選擇它時,我需要填充該組合框,但無法使其工作。將Combobox綁定到ViewModelMain方法

我的ViewModel看起來像這樣,我感興趣的是GetMessagesTypes()方法。messageType變量返回我需要綁定到組合框的MessageType列表。

任何指針,將不勝感激。

namespace Toolbox.ViewModel 
{ 
    [ImplementPropertyChanged] 
    internal class ViewModelMain 
    { 
     #region Fields 
     private readonly IActionLogRepository m_ActionLogRepository; 
     #endregion 

     #region Properties 
     public DateTime QueryFromDate { get; set; } 
     public DateTime QueryToDate { get; set; } 
     public int TopXRecords { get; set; } 
     public ICommand SearchTopXRecord { get; private set; } 
     public ICommand GetListOfmessageTypes { get; set; } 
     public ICommand SearchDateCommand { get; private set; } 
     public object SelectedMessageBody { get; set; } 
     public ObservableCollection<IActionLog> Messages { get; set; } 

     #endregion 

     #region Constructor 
     //Should use injection container 
     public ViewModelMain(IActionLogRepository actionLogRepository) 
     { 
      QueryToDate = DateTime.Now; 
      QueryFromDate = DateTime.Now.Subtract(TimeSpan.FromDays(1)); 
      m_ActionLogRepository = actionLogRepository; 
      Messages = new ObservableCollection<IActionLog>(); 
      SearchDateCommand = new SimpleCommand { ExecuteDelegate = SetActionLogsBetweenDates }; 
      SearchTopXRecord = new SimpleCommand { ExecuteDelegate = SetActionLogsForTopXRecords }; 

      SetActionLogs(); 
      //GetMessagesTypes(); 
     } 
     #endregion 

     #region Methods 
     private void SetActionLogs() 
     { 
      List<IActionLog> actionLogs = m_ActionLogRepository.GetAllActionLogs(); 

      Messages.Clear(); 
      actionLogs.ForEach(actionLog => Messages.Add(actionLog)); 
     } 

     public void SetActionLogsBetweenDates() 
     { 
      List<IActionLog> actionLogs = m_ActionLogRepository.GetAllActionLogsBetweenDates(QueryFromDate, QueryToDate); 

      Messages.Clear(); 
      actionLogs.ForEach(actionLog => Messages.Add(actionLog)); 
     } 

     public void SetActionLogsForTopXRecords() 
     { 
      List<IActionLog> actionLogs = m_ActionLogRepository.GetAllTopXActionLogs(TopXRecords); 

      Messages.Clear(); 
      actionLogs.ForEach(actionLog => Messages.Add(actionLog)); 
     } 

     public string GetMessagesTypes() 
     { 

      List<IActionLog> actionLogMessageType = m_ActionLogRepository.GetAllActionLogs(); 

      var messageType = (
       from messageTypes in actionLogMessageType 
       select messageTypes.MessageType).Distinct(); 
      return messageType.ToString(); //Return Messages types 
     } 

     #endregion 
    } 
} 

回答

1

在WPF中,我們沒有數據綁定方法的結果。相反,你有幾個選擇要做......事情是你需要在視圖模型中執行,以便你可以調用你的方法。調用方法後,應該將結果值設置爲數據綁定到ComboBox.ItemsSource屬性的集合屬性。

您可以在視圖模型中執行的一種方法是將數據綁定到SelectedItem屬性(或類似屬性)的屬性。每次選擇的項目更改時,執行都將轉到視圖模型,並且可以調用您的方法。拿這個小例子:

private YourDataType selectedItem = new YourDataType(); 
public YourDataType SelectedItem 
{ 
    get { return selectedItem; } 
    set 
    { 
     selectedItem = value; 
     NotifyPropertyChanged("SelectedItem"); 
     DoSomethingWithSelectedItem(SelectedItem); // <-- Call method here 
    } 
} 

另一種方式來獲得進入執行在正確的點視圖模型可能是實現某種ICommand是被一組特定的情況下......這是給你下解僱,因爲只要您在正確的時間執行視圖模型,就可以調用您的方法並將結果填充到public集合屬性中。

你沒有很清楚地解釋你的情況,所以這個解決方案可能不適合你,但是你應該明白這個想法並且能夠從中抽象出一個解決方案。