Ответ 1
Это работает для меня в UWP:
<Button Command="{Binding CheckWeatherCommand}">
<Button.CommandParameter>
<local:WeatherEnum>Cold</local:WeatherEnum>
<Button.CommandParameter>
</Button>
Приложение, над которым я работаю, требует, чтобы конвертер был переименован. Для этого регулярный способ сделать:
{Binding whatever,
Converter={StaticResource converterName},
ConverterParameter={x:Static namespace:Enum.Value}}
Однако пространство имен xW платформы UWP, похоже, не имеет расширения Static.
Кто-нибудь знает, есть ли решение, которое не полагается на x: Static для сравнения перечисления в привязке?
Это работает для меня в UWP:
<Button Command="{Binding CheckWeatherCommand}">
<Button.CommandParameter>
<local:WeatherEnum>Cold</local:WeatherEnum>
<Button.CommandParameter>
</Button>
На UWP (и платформе WinRT) нет расширения статической разметки.
Одним из возможных решений является создание класса с значениями enum в качестве свойств и сохранение экземпляра этого класса в ResourceDictionary.
Пример:
public enum Weather
{
Cold,
Hot
}
Вот наш класс с значениями перечисления:
public class WeatherEnumValues
{
public static Weather Cold
{
get
{
return Weather.Cold;
}
}
public static Weather Hot
{
get
{
return Weather.Hot;
}
}
}
В вашем ресурсе:
<local:WeatherEnumValues x:Key="WeatherEnumValues" />
И вот мы:
"{Binding whatever, Converter={StaticResource converterName},
ConverterParameter={Binding Hot, Source={StaticResource WeatherEnumValues}}}" />
Самый сжатый способ, которым я знаю...
public enum WeatherEnum
{
Cold,
Hot
}
Определите значение перечисления в XAML:
<local:WeatherEnum x:Key="WeatherEnumValueCold">Cold</local:WeatherEnum>
И просто используйте его:
"{Binding whatever, Converter={StaticResource converterName},
ConverterParameter={StaticResource WeatherEnumValueCold}}"
Это ответ с использованием ресурсов и без преобразователей:
Вид
<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
.........................
}