Silverlight: How do I pass True to a CommandParameter?
-
13-04-2021 - |
Question
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.
La solution
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}" />
Autres conseils
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}" />