x:Static in UWP XAML

梦想的初衷 提交于 2019-11-28 07:29:28

This works for me in a UWP:

<Button Command="{Binding CheckWeatherCommand}">
  <Button.CommandParameter>
     <local:WeatherEnum>Cold</local:WeatherEnum>
  <Button.CommandParameter>
</Button>

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}}}" />

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}}"

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
     .........................
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!