I have an Enum of "Invoice Actions" that populate a combobox, but i would like to disable one of the enum members ("View") based on a "CanDisplayDetails" property that is in the ViewModel.
I can't quite figure out how to bind the IsEnabled property to "CanDisplayDetails" in a ComboBoxItem ItemContainerStyle, since the context seems to be the Enum instead of my ViewModel. How do i correct the binding so that it can provide the enum value, but IsEnabled binds to my ViewModel property? And it also needs to only effect the "View" ComboBoxItem. Thanks!
Style idea so far:
<ComboBox.ItemContainerStyle>
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="IsEnabled" Value="{Binding CanDisplayDetails}"/>
</Style>
</ComboBox.ItemContainerStyle>
In the XAML UserControl's Resources:
<ObjectDataProvider x:Key="ActionsEnum" MethodName="GetValues" ObjectType="{x:Type sys:Enum}">
<ObjectDataProvider.MethodParameters>
<x:TypeExtension TypeName="Constants:InvoiceActionsLong"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
The ComboBox in the same UserControl:
<ComboBox Grid.Column="1" ItemsSource="{Binding Source={StaticResource ActionsEnum}}" IsEnabled="{Binding SelectedItem, Converter={StaticResource SelectionConverter}}"
SelectedItem="{Binding SelectedAction}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource EnumDescriptionConverter}}" />
</DataTemplate>
</ComboBox.ItemTemplate>
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding ActionCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
The EnumDescriptionConverter:
public class EnumDescriptionConverter : IValueConverter
{
/// <summary>
/// Get enum description
/// </summary>
/// <param name="enumObj"></param>
/// <returns></returns>
private string GetEnumDescription(Enum enumObj)
{
FieldInfo fieldInfo = enumObj.GetType().GetField(enumObj.ToString());
object[] attribArray = fieldInfo.GetCustomAttributes(false);
if (attribArray.Length == 0)
{
return enumObj.ToString();
}
else
{
DescriptionAttribute attrib = attribArray[0] as DescriptionAttribute;
return attrib.Description;
}
}
/// <summary>
/// Returns an enum member's description
/// </summary>
/// <param name="value"></param>
/// <param name="targetType"></param>
/// <param name="parameter"></param>
/// <param name="culture"></param>
/// <returns></returns>
object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Enum myEnum = (Enum)value;
string description = GetEnumDescription(myEnum);
return description;
}
object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return string.Empty;
}
}
The enum:
/// <summary>
/// Enum lists all available Actions for Action Combo Box at the Invoice level for DETAIL types ie. active, plan
/// </summary>
public enum InvoiceActionsLong
{
[Description("Contacts")]
Contacts,
[Description("Delivery")]
Delivery,
[Description("Documents")]
Documents,
[Description("Note")]
Note,
[Description("Payments")]
Payments,
[Description("Print")]
Print,
[Description("Process")]
Process,
[Description("Reload")]
Reload,
[Description("Send")]
Send,
[Description("View")]
View
}
The property in my ViewModel:
/// <summary>
/// Bool denotes whether can display details
/// </summary>
private bool CanDisplayDetails
{
get
{
bool b = true;
if (SelectedItem != null)
{
if (SelectedItem.AdjustmentTypeID == 1)
{
b = false;
}
}
return b;
}
}