Passing an Enum Value as Command Parameter from Xaml

Passing an enum value as command parameter from XAML

Try this

<Button CommandParameter="{x:Static local:SearchPageType.First}" .../>

local - is your namespace reference in the XAML

Passing Enum Value as a Command Parameter

Not sure I understand your requirement correctly... is this what you want?

CommandParameter="{Binding Path={x:Static local:TestEnum.First}}"

EDIT: ok, I think I understand now... If you want the enum values as the ItemsSource, you could do it with an ObjectDataProvider, but there's a better way: write a markup extension that takes in the type of the enum and returns the values.

Markup extension

[MarkupExtensionReturnType(typeof(Array))]
public class EnumValuesExtension : MarkupExtension
{
public EnumValuesExtension()
{
}

public EnumValuesExtension(Type enumType)
{
this.EnumType = enumType;
}

[ConstructorArgument("enumType")]
public Type EnumType { get; set; }

public override object ProvideValue(IServiceProvider serviceProvider)
{
return Enum.GetValues(EnumType);
}
}

XAML

<MenuItem ItemsSource="{my:EnumValues EnumType=my:TestEnum}" Name="menu">
<MenuItem.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter Property="Header" Value="{Binding}" />
<Setter Property="Command" Value="{Binding SomeCommand, ElementName=menu}" />
<Setter Property="CommandParameter" Value="{Binding}" />
</Style>
</MenuItem.ItemContainerStyle>
</MenuItem>

Pass Enum value as CommandParameter when the Enum is in the ViewModel

This work fine:

CommandParameter="{x:Static uiTest:MainWindow+LocationLabelType.Location}"

You ran the project with this code?
WPF designer can show this error //'Type was not found.' if you don't build the project, because it does not see the type of enum.

WPF Binding Enum to Command Parameters

Yes, using a ValueConverter would be the right thing to do. This thread has en example you can use.

Silverlight 3/Prism - Passing an enum value as a Command Parameter

Binding enums are really a troublesome operation, even in WPF. But there seems to be an elegant solution to it, which is available at the Caliburn framework.

The solution, however, is not available in the framework code, but in it's LOB Samples. Look for the BindableEnum and BindableEnumCollection<T> classes on the Caliburn.Silverlight.ApplicationFramework project.

HTH.

Passing an enum value as command parameter when using MultiBinding

Change Path to Source in <Binding Path="{x:Static vm:Direction.Down}" Mode="OneWay"/>:

<Button.CommandParameter>
<MultiBinding Converter="{StaticResource commandParametersConverter}">
<MultiBinding.Bindings>
<Binding Path="DataContext" />
<Binding Source="{x:Static vm:Direction.Down}" Mode="OneWay"/>
</MultiBinding.Bindings>
</MultiBinding>
</Button.CommandParameter>


Related Topics



Leave a reply



Submit