我有一個組合框,當用戶從存儲在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
}
}