XAML의 부울 CommandParameter
이 코드가 있습니다 (잘 작동합니다).
<KeyBinding Key="Enter" Command="{Binding ReturnResultCommand}">
<KeyBinding.CommandParameter>
<s:Boolean>
True
</s:Boolean>
</KeyBinding.CommandParameter>
</KeyBinding>
"s"는 물론 System 네임 스페이스입니다.
그러나이 명령은 여러 번 호출되며 실제로는 다소 단순한 XAML 코드를 부풀립니다. 이것은 XAML에서 부울 명령 매개 변수의 가장 짧은 표기법입니까 (명령을 여러 명령으로 분할하는 것 제외)?
이것은 약간의 해킹 일 수 있지만 KeyBinding
클래스 에서 파생 될 수 있습니다 .
public class BoolKeyBinding : KeyBinding
{
public bool Parameter
{
get { return (bool)CommandParameter; }
set { CommandParameter = value; }
}
}
용법:
<local:BoolKeyBinding ... Parameter="True"/>
그리고 그리 이상하지 않은 또 다른 해결책 :
xmlns:s="clr-namespace:System;assembly=mscorlib"
<Application.Resources>
<!-- ... -->
<s:Boolean x:Key="True">True</s:Boolean>
<s:Boolean x:Key="False">False</s:Boolean>
</Application.Resources>
용법:
<KeyBinding ... CommandParameter="{StaticResource True}"/>
가장 쉬운 방법은 리소스에서 다음을 정의하는 것입니다.
<System:Boolean x:Key="FalseValue">False</System:Boolean>
<System:Boolean x:Key="TrueValue">True</System:Boolean>
다음과 같이 사용하십시오.
<Button CommandParameter="{StaticResource FalseValue}"/>
이 태그 확장으로 훨씬 더 일반적인 솔루션을 찾았습니다.
public class SystemTypeExtension : MarkupExtension
{
private object parameter;
public int Int{set { parameter = value; }}
public double Double { set { parameter = value; } }
public float Float { set { parameter = value; } }
public bool Bool { set { parameter = value; } }
// add more as needed here
public override object ProvideValue(IServiceProvider serviceProvider)
{
return parameter;
}
}
사용법 ( "wpf :"는 확장이있는 네임 스페이스입니다) :
<KeyBinding Key="F8" Command="{Binding SomeCommand}" CommandParameter="{wpf:SystemType Bool=True}"/>
당신은 옵션을 얻을 True
하고 False
입력 한 후 Bool=
입력 안전!
또는 다음과 같습니다.
<Button.CommandParameter>
<s:Boolean>True</s:Boolean>
</Button.CommandParameter>
여기서 s는 네임 스페이스입니다.
xmlns:s="clr-namespace:System;assembly=mscorlib"
Perhaps something like
<KeyBinding Key="Enter" Command="{Binding ReturnResultCommand}"
CommandParameter="{x:Static StaticBoolean.True}" />
where StaticBoolean
is
public static class StaticBoolean
{
public static bool True
{
get { return true; }
}
}
Here's another approach where you define your own markup extensions that return True
or False
(or any other value you wish). Then you simply use them right in XAML like any other markup extension:
public class TrueExtension : MarkupExtension {
public override object ProvideValue(IServiceProvider serviceProvider) => true;
}
public class FalseExtension : MarkupExtension {
public override object ProvideValue(IServiceProvider serviceProvider) => false;
}
public class DoubleExtension : MarkupExtension {
public DoubleExtension(){};
public DoubleExtension(double value) => Value = value;
public double Value { get; set; }
public override object ProvideValue(IServiceProvider serviceProvider) => Value;
}
You then use them like this (assuming your imported namespace is mx
):
<KeyBinding Key="Enter"
Command="{Binding ReturnResultCommand}"
CommandParameter="{mx:True}" />
<Button Visibility="{Binding SomeProperty,
Converter={SomeBoolConverter},
ConverterParameter={mx:True}}">
<!-- This guarantees the value passed is a double equal to 42.5 -->
<Button Visibility="{Binding SomeProperty,
Converter={SomeDoubleConverter},
ConverterParameter={mx:Double 42.5}}">
I actually define lots of custom MarkupExtension
classes for a lot of common things that I don't want to necessarily store in my resources.
참고URL : https://stackoverflow.com/questions/4997446/boolean-commandparameter-in-xaml
'developer tip' 카테고리의 다른 글
테이블 행 수를 얻는 가장 효율적인 방법 (0) | 2020.11.16 |
---|---|
'MyException'변수가 선언되었지만 사용되지 않았습니다. (0) | 2020.11.16 |
하이 차트 차트에서 모든 계열 데이터를 제거하는 올바른 방법은 무엇입니까? (0) | 2020.11.16 |
Express.js-헤더가 이미 전송되었는지 확인하는 방법은 무엇입니까? (0) | 2020.11.16 |
Lodash를 Angular JS와 함께 사용하는 방법은 무엇입니까? (0) | 2020.11.16 |