I am learning WPF and MVVM again. I have an application in which I pass a command with a parameter that as I understand can only be turned into a string? I need to somehow turn that string into an enum value. What can I do?
As part of my model, a class that contains an array of another model object, each with a name property. The array was created using the values of the enumeration as indices. Basically, I have a year composed of months and those months are to contain financial
data.
/// <summary>
/// Months in a year
/// </summary>
public enum Month
{
January,
February,
March,
April,
May,
June,
July,
August,
September,
October,
November,
December
};
...
//------------------------------------------------------------------------------
public class FinancialYear
{
FinancialMonth[] m_financialMonths;
/// <summary>
/// Constructor
/// </summary>
/// <param name="year"></param>
public FinancialYear(int year)
{
if (year < 1900 || year > 9999)
{
throw new ArgumentException("Year argument must be between 1900 and 9999 inclusive.");
}
Year = year;
string[] monthNames = Enum.GetNames(typeof(Month));
int numberOfMonths = monthNames.Length;
m_financialMonths = new FinancialMonth[numberOfMonths];
for (int index = 0; index < numberOfMonths; ++index)
{
m_financialMonths[index] = new FinancialMonth(monthNames[index]);
}
}
...
Now my problem is that I have a collection of view models in my main window view model and want to navigate to them using a number of buttons that I have created. How do I pass "which month" from the view to the viewmodel since I really am looking for an enum type? What goes in the below?
<Button x:Name="btnJan" Content="Jan" Command="{Binding ChangeContentCommand}" CommandParameter="January" Style="{StaticResource NavButton}" Margin="2,2,2,2" Padding="8,4,8,4"/>
<Button x:Name="btnFeb" Content="Feb" Command="{Binding ChangeContentCommand}" CommandParameter="February"Style="{StaticResource NavButton}"
Margin="2,2,2,2" Padding="8,4,8,4"/>
etc. etc.
// <summary>
/// Method executed by the ChangeContentCommand
/// </summary>
/// <param name="parameter">Optional parameter send with the command</param>
private void ChangeContent(object parameter)
{
if (parameter != null)
{
string commandParam = parameter.ToString();
}
}
Edit:
The question could be simply: How do I convert a string to an enum value. But, I'd also like input on whether or not I should be using an enum this way at all, if some other collection would be better suited, if I am just way off base on how I am doing this or not, etc. I come from C++ so this use of an enumeration seems natural to me, but it seems .NET enums are somewhat different.