uwpuwp-xamlischecked

Change IsChecked property on ChangePropertyAction [uwp]


I need to change the IsChecked property of a Radio Button on the click of a simple Button in XAML using ChangePropertyAction.
I am able to change every property of the radio button (Content, Visibility etc) through this method except IsChecked and it gives a unnamed error.

<Button x:Name="button1">
    <Interactivity:Interaction.Behaviors>
        <Core:EventTriggerBehavior EventName="Click" SourceObject="{Binding ElementName=button1}">
            <Core:ChangePropertyAction TargetObject="{Binding ElementName=radio1}" PropertyName="IsChecked" Value="false"/>
        </Core:EventTriggerBehavior>
    </Interactivity:Interaction.Behaviors>
</Button>

May I know the reason and the solution please? Thanks.


Solution

  • The problem seems due to this, checkbox ischecked property is inherit from its base class ToggleButton. And it's value is Nullable(See details from this doc). And that's why it reports exception like the following:

    Windows.UI.Xaml.Markup.XamlParseException: The text associated with this error code could not be found. name expected [Line: 1 Position: 175] at Windows.UI.Xaml.Markup.XamlReader.Load(String xaml) at Microsoft.Xaml.Interactions.Core.TypeConverterHelper.Convert(String value, String destinationTypeFullName)

    To solve this problem, try create a converter yourself:

     public class NullReaderConverter:IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            return value;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
    

    Then on your XAML, try the following:

     <Grid>
        <Grid.Resources>
            <local:NullReaderConverter x:Name="myconverter"/>
            <x:Boolean x:Key="falsevalue">false</x:Boolean>
        </Grid.Resources>
        <RelativePanel>
            <CheckBox Content="test1" x:Name="radio1" IsChecked="True"></CheckBox>
            <Button x:Name="button1" Content="change" RelativePanel.Below="radio1">
                <Interactivity:Interaction.Behaviors>
                    <Interactions:EventTriggerBehavior EventName="Click" SourceObject="{Binding ElementName=button1}">
                        <Interactions:ChangePropertyAction TargetObject="{Binding ElementName=radio1}" PropertyName="IsChecked" Value="{Binding Converter={StaticResource myconverter},Source={StaticResource falsevalue}}"/>
                    </Interactions:EventTriggerBehavior>
                </Interactivity:Interaction.Behaviors>
            </Button>
        </RelativePanel>
    
    </Grid>