programing

XAML의 부울 명령어파라미터

fastcode 2023. 4. 15. 09:30
반응형

XAML의 부울 명령어파라미터

다음 코드가 있습니다(적절하게 동작합니다).

<KeyBinding Key="Enter" Command="{Binding ReturnResultCommand}">
    <KeyBinding.CommandParameter>
        <s:Boolean>
            True
        </s:Boolean>
    </KeyBinding.CommandParameter>
</KeyBinding>

여기서 "s"는 물론 시스템 이름 공간입니다.

그러나 이 명령어는 여러 번 호출되며 실제로는 단순한 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}"/>

가장 쉬운 방법은 [Resources]

<System:Boolean x:Key="FalseValue">False</System:Boolean>
<System:Boolean x:Key="TrueValue">True</System:Boolean>

다음과 같이 사용합니다.

<Button CommandParameter="{StaticResource FalseValue}"/>

또는 다음과 같은 경우도 있습니다.

<Button.CommandParameter>
    <s:Boolean>True</s:Boolean>
</Button.CommandParameter>

여기서 s는 네임스페이스입니다.

 xmlns:s="clr-namespace:System;assembly=mscorlib"

나는 이 마크업 확장자를 가진 훨씬 더 일반적인 솔루션을 발견했다.

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=활자 안전!

아마 뭐랄까

<KeyBinding Key="Enter" Command="{Binding ReturnResultCommand}"
    CommandParameter="{x:Static StaticBoolean.True}" />

어디에StaticBoolean

public static class StaticBoolean
{
    public static bool True
    {
        get { return true; }
    }
}

여기 또 다른 접근법이 있습니다.이 접근법에서는 마크업 확장을 정의하면True또는False(또는 원하는 다른 값).그런 다음 다른 마크업 확장과 마찬가지로 XAML에서 바로 사용할 수 있습니다.

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

그런 다음 다음과 같이 사용합니다(가져온 네임스페이스가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}}">

실제로 많은 커스텀을 정의하고 있습니다.MarkupExtension자원에 꼭 저장해 두고 싶지 않은 많은 일반적인 것들을 위한 수업입니다.

언급URL : https://stackoverflow.com/questions/4997446/boolean-commandparameter-in-xaml

반응형