Silverlight: How do I pass True to a CommandParameter?
Asked Answered
S

3

5

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.

Selfpronouncing answered 4/1, 2012 at 4:47 Comment(0)
M
12

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}" />
Marleen answered 26/4, 2013 at 2:4 Comment(0)
A
12

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"
Asymmetric answered 4/1, 2012 at 6:17 Comment(0)
M
12

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}" />
Marleen answered 26/4, 2013 at 2:4 Comment(0)
S
5

your XAML changes to this.

<Button 
    Command="{Binding Path=WhateverCommand}" 
    CommandParameter="{x:Static BooleanHelper.True}" />
Sanbo answered 29/6, 2016 at 7:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.