Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set RadioButton Checked in code with caliburn micro

I have a Page inside a Wizard with 4 Radio Buttons (2 Groups) in a WPF Application. I'm using .Net4 and Caliburn Micro.

When click and Set a Value it's properly bound to the corresponding property. When i leave the Page and return, i need to set the properties from in code and expect them via NotifyPropertyChanged to be updated on the Page. But non of the RadioButtons gets checked, even though the corresponding property gets set...

Does anyone know how that should work with caliburn.micro?!

here's the xaml:

<RadioButton Name="NewInstallChecked" GroupName="InstallType" Content="New Installation (default)" Margin="75,10,0,0" />
<RadioButton Name="UpdateInstallChecked" GroupName="InstallType" Content="Update of existing Installation" Margin="75,10,0,0" />
<Label Content="Please select which version of Siseco you want to install:" Height="28" HorizontalAlignment="Left" Margin="20,20,0,0" Name="label2" VerticalAlignment="Top" />
<RadioButton Name="ServerChecked" GroupName="Version" Content="Server version (default)" Margin="75,10,0,0" />
<RadioButton Name="ClientChecked" GroupName="Version" Content="Client version" Margin="75,10,0,0" />

and here the code in my view model:

        public bool ClientChecked
    {
        get { return _clientChecked; }
        set { _clientChecked = value; NotifyOfPropertyChange(() => ClientChecked); }
    }

    public bool ServerChecked
    {
        get { return _serverChecked; }
        set { _serverChecked = value; NotifyOfPropertyChange(() => ServerChecked); }
    }

    public bool NewInstallChecked
    {
        get { return _newInstallChecked; }
        set { _newInstallChecked = value; NotifyOfPropertyChange(() => NewInstallChecked); }
    }

    public bool UpdateInstallChecked
    {
        get { return _updateInstallChecked; }
        set { _updateInstallChecked = value; NotifyOfPropertyChange(() => UpdateInstallChecked);}
    }

...

    protected override void OnActivate()
    {
        NewInstallChecked = _options.NewInstall;
        UpdateInstallChecked = _options.UpdateInstall;
        ServerChecked = _options.ServerInstall;
        ClientChecked = _options.ClientInstall;
        base.OnActivate();
    }
like image 749
JonSchn Avatar asked Jan 15 '13 15:01

JonSchn


People also ask

How do you check whether a RadioButton is checked or not in android?

Use getCheckedRadioButtonId() method on your RadioGroup to find out. It returns -1 when no RadioButton in the group is selected.

How do you programmatically determine whether a RadioButton is checked?

You can check the current state of a radio button programmatically by using isChecked() method. This method returns a Boolean value either true or false. if it is checked then returns true otherwise returns false.

How do you create a dynamic radio button?

For creating dynamic RadioButton, we need to use android. view. ViewGroup. LayoutParams which configures the width and height of views and implements setOnCheckedChangeListener() method of RadioGroup class.


1 Answers

I didn't have any trouble getting this to work, so I hope I understand your question correctly. Here's the code I used:

The View Model

public class RadioButtonTestViewModel : Screen
{
    private bool newInstallChecked;
    private bool updateInstallChecked;
    private bool serverChecked;
    private bool clientChecked;

    public bool NewInstallChecked
    {
        get { return newInstallChecked; }
        set
        {
            if (value.Equals(newInstallChecked)) return;
            newInstallChecked = value;
            NotifyOfPropertyChange(() => NewInstallChecked);
        }
    }

    public bool UpdateInstallChecked
    {
        get { return updateInstallChecked; }
        set
        {
            if (value.Equals(updateInstallChecked)) return;
            updateInstallChecked = value;
            NotifyOfPropertyChange(() => UpdateInstallChecked);
        }
    }

    public bool ServerChecked
    {
        get { return serverChecked; }
        set
        {
            if (value.Equals(serverChecked)) return;
            serverChecked = value;
            NotifyOfPropertyChange(() => ServerChecked);
        }
    }

    public bool ClientChecked
    {
        get { return clientChecked; }
        set
        {
            if (value.Equals(clientChecked)) return;
            clientChecked = value;
            NotifyOfPropertyChange(() => ClientChecked);
        }
    }

    public void SaveAndClose()
    {
        Options.Client = ClientChecked;
        Options.NewInstall = NewInstallChecked;

        Options.Server = ServerChecked;
        Options.UpdateInstall = UpdateInstallChecked;

        TryClose();
    }

    protected override void OnInitialize()
    {
        base.OnInitialize();

        ClientChecked = Options.Client;
        NewInstallChecked = Options.NewInstall;

        ServerChecked = Options.Server;
        UpdateInstallChecked = Options.UpdateInstall;
    }

    public static class Options
    {
        public static bool NewInstall { get; set; }
        public static bool UpdateInstall { get; set; }

        public static bool Server { get; set; }
        public static bool Client { get; set; }
    }
}

The View

<UserControl x:Class="CaliburnMicroTest.Views.RadioButtonTestView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             d:DesignHeight="300"
             d:DesignWidth="300"
             mc:Ignorable="d">
    <StackPanel>
        <RadioButton Name="NewInstallChecked"
                     Margin="75,10,0,0"
                     Content="New Installation (default)"
                     GroupName="InstallType" />
        <RadioButton Name="UpdateInstallChecked"
                     Margin="75,10,0,0"
                     Content="Update of existing Installation"
                     GroupName="InstallType" />
        <Label Name="label2"
               Height="28"
               Margin="20,20,0,0"
               HorizontalAlignment="Left"
               VerticalAlignment="Top"
               Content="Please select which version of Siseco you want to install:" />
        <RadioButton Name="ServerChecked"
                     Margin="75,10,0,0"
                     Content="Server version (default)"
                     GroupName="Version" />
        <RadioButton Name="ClientChecked"
                     Margin="75,10,0,0"
                     Content="Client version"
                     GroupName="Version" />
        <StackPanel Margin="10" Orientation="Horizontal">
            <Button Name="SaveAndClose"
                    Width="80"
                    Content="Ok" />
            <Button Name="TryClose"
                    Width="80"
                    Content="Cancel" />
        </StackPanel>
    </StackPanel>
</UserControl>

With above code, I was able to set the values of the radio buttons, close the view, and re-open it while having the radio button values persist. Hope that helps!

like image 155
Brandon Baker Avatar answered Sep 28 '22 09:09

Brandon Baker