Set RadioButton Checked in code with caliburn micro
Asked Answered
S

1

5

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();
    }
Shelleyshellfire answered 15/1, 2013 at 15:51 Comment(7)
Just out of curiosity - have you tried it without the radiobuttons in groups? I'm just wondering if the 'group' mechanism is somehow interfering with the bindings - technically you should let the binding resolve the initial value - you shouldn't fire a propertychanged event upon initialisation (it's a property changed event, not a property initialised event). Also try setting the checked bool values in the constructor of the VM and see if that makes a differenceSext
Does it still not work if you set them in OnViewLoaded instead?Rosewater
Good point - likely that the INPC is firing before the view is attached which means there's nothing to update on the bindings, which would mean that what I've suggested may be useless! :)Sext
tried the option with setting it in OnViewLoaded. Didn't help.Shelleyshellfire
i also tried the solution with not setting the property and therefore not firing property changed event, although i don't think it's an "real" initialisation, because it's not a new instance of the Page, it just gets reactivated... but it anyways didn't work like that too...Shelleyshellfire
and i also tried using native binding like IsChecked="{Binding Path=ClientChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Shelleyshellfire
Weird - I'll try it myself in a test project, I don't really remember using radio buttons in WPF (ever!) thoughSext
B
11

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!

Buckling answered 3/2, 2013 at 18:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.