29

An app I'm working on requires a ConverterParameter to be an enum. For this, the regular way to do would be:

{Binding whatever, 
    Converter={StaticResource converterName}, 
    ConverterParameter={x:Static namespace:Enum.Value}}

However, the UWP platform x: namespace does not seem to have the Static extension.

Does anyone know if there's a solution that does not rely on x:Static for comparing an enum in binding?

fonix232
  • 2,132
  • 6
  • 39
  • 69
  • 1
    I haven't done any UWP, so I don't know, but there's [this article](http://www.codeproject.com/Articles/305932/Static-and-Type-markup-extensions-for-Silverlight) explaining how to make a markup extension for Silverlight (which doesn't have `x:Static` either). It may apply here – Jcl Aug 12 '15 at 11:46
  • Here's [answer](http://stackoverflow.com/a/360639/1526778) about using enum in xaml that must work – Dmitry Ponomarenko Nov 22 '15 at 19:27

5 Answers5

47

This works for me in a UWP:

<Button Command="{Binding CheckWeatherCommand}">
  <Button.CommandParameter>
     <local:WeatherEnum>Cold</local:WeatherEnum>
  <Button.CommandParameter>
</Button>
user5971245
  • 486
  • 5
  • 2
  • 2
    Wow, this needs to be upvoted more. That works perfectly, no workarounds needed with classes and what not. – René Sackers Apr 02 '16 at 21:51
  • 2
    Is there any way to specify this in line via `CommandParameter=...`, to avoid the xaml in the button body? – Felix Sep 15 '16 at 17:50
  • 5
    @Felix Seems to work fine declaring the type as a resource. Cold then referenced anywhere CommandParameter={StaticResource Cold}. Not as clean as x:Static and inline but it saves adding the XAML wrappers everytime. – Michael Puckett II Apr 13 '17 at 18:47
  • I'm getting unable to cast .... to object of type .... error tooltip in XAML. And in runtime the value is null. – babgev Oct 29 '17 at 23:44
11

The most concise way I know of...

public enum WeatherEnum
{
    Cold,
    Hot
}

Define the enum value in XAML:

<local:WeatherEnum x:Key="WeatherEnumValueCold">Cold</local:WeatherEnum>

And simply use it:

"{Binding whatever, Converter={StaticResource converterName},
 ConverterParameter={StaticResource WeatherEnumValueCold}}"
Stefan Ahlm
  • 268
  • 4
  • 11
  • That doesn't work for me... I tried as you suggested, but on runtime I get this error: The TypeConverter for "Phase" does not support converting from a string. (Phase is my enumerator) – II ARROWS Mar 11 '16 at 20:34
  • This doesn't work me either. It throws a conversion error – Xam Jan 18 '20 at 16:30
9

There is no Static Markup Extension on UWP (and WinRT platform too).

One of the possible solutions is to create class with enum values as properties and store instance of this class in the ResourceDictionary.

Example:

public enum Weather
{
    Cold,
    Hot
}

Here is our class with enum values:

public class WeatherEnumValues
{
    public static Weather Cold
    {
        get
        {
            return Weather.Cold;
        }
    }

    public static Weather Hot
    {
        get
        {
            return Weather.Hot;
        }
    }
}

In your ResourceDictionary:

<local:WeatherEnumValues x:Key="WeatherEnumValues" />

And here we are:

"{Binding whatever, Converter={StaticResource converterName},
 ConverterParameter={Binding Hot, Source={StaticResource WeatherEnumValues}}}" />
RavingDev
  • 2,817
  • 1
  • 17
  • 19
1

This is an answer utilizing resources and without Converters:

View:

<Page
.....
xmlns:local="using:EnumNamespace"
.....
>
  <Grid>
    <Grid.Resources>
        <local:EnumType x:Key="EnumNamedConstantKey">EnumNamedConstant</local:SettingsCats>
    </Grid.Resources>

    <Button Content="DoSomething" Command="{Binding DoSomethingCommand}" CommandParameter="{StaticResource EnumNamedConstantKey}" />
  </Grid>
</Page>

ViewModel

    public RelayCommand<EnumType> DoSomethingCommand { get; }

    public SomeViewModel()
    {
        DoSomethingCommand = new RelayCommand<EnumType>(DoSomethingCommandAction);
    }

    private void DoSomethingCommandAction(EnumType _enumNameConstant)
    {
     // Logic
     .........................
    }
usefulBee
  • 9,250
  • 10
  • 51
  • 89
0

x:Bind is a drop-in replacement for x:Static, so you can do:

ConverterParameter="{x:Bind namespace:Enum.Value}}"
Mike Marynowski
  • 3,156
  • 22
  • 32