可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
This works for me in a UWP:
回答2:
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:
And here we are:
"{Binding whatever, Converter={StaticResource converterName}, ConverterParameter={Binding Hot, Source={StaticResource WeatherEnumValues}}}" />
回答3:
The most concise way I know of...
public enum WeatherEnum { Cold, Hot }
Define the enum value in XAML:
Cold
And simply use it:
"{Binding whatever, Converter={StaticResource converterName}, ConverterParameter={StaticResource WeatherEnumValueCold}}"
回答4:
This is an answer utilizing resources and without Converters:
View:
EnumNamedConstant
ViewModel
public RelayCommand DoSomethingCommand { get; } public SomeViewModel() { DoSomethingCommand = new RelayCommand(DoSomethingCommandAction); } private void DoSomethingCommandAction(EnumType _enumNameConstant) { // Logic ......................... }