문제

How do I pass True to a CommandParameter?

Currently I am imperatively adding Boolean.True to the resource dictionary, but that seems like a clumsy way to do it.

도움이 되었습니까?

해결책

ColinE's answer is fine, but I think it's a bit neater to define the true/false as resources. You only have to do that once:

<UserControl.Resources>
    <sys:Boolean x:Key="BoolTrue">True</sys:Boolean>
    <sys:Boolean x:Key="BoolFalse">False</sys:Boolean>
</UserControl.Resources>

Then you can reference it as a StaticResource for the CommandParameter:

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

다른 팁

Because command parameters are of type 'object' the XAML parser is unable to perform type conversion for you. If you pass 'true', the parser has no way of knowing that you want this converted to a boolean value. You will have to do this explicitly. You could use the property element syntax:

<Button>
  <Button.CommandParameter>
    <sys:Boolean>true</sys:Boolean>
  </Button.CommandParameter>
</Button>

Where the sys namepsace is mapped:

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

your XAML changes to this.

<Button 
    Command="{Binding Path=WhateverCommand}" 
    CommandParameter="{x:Static BooleanHelper.True}" />
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top