设置单选按钮检查代码与caliburn微

本文关键字:caliburn 代码 单选按钮 检查 设置 | 更新日期: 2023-09-27 17:53:32

我在WPF应用程序中有一个带有4个单选按钮(2组)的向导中的页面。我正在使用。net 4和Caliburn Micro。

当点击并设置一个值时,它被正确地绑定到相应的属性。当我离开页面并返回时,我需要从代码中设置属性,并期望通过NotifyPropertyChanged在页面上更新它们。但是没有一个radiobutton被选中,即使相应的属性被设置…

有谁知道caliburn.micro应该如何工作吗?!

这里是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" />

和这里的代码在我的视图模型:

        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();
    }

设置单选按钮检查代码与caliburn微

我没有任何麻烦得到这个工作,所以我希望我正确理解你的问题。下面是我使用的代码:

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; }
    }
}

<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>
使用上面的代码,我能够设置单选按钮的值,关闭视图,然后重新打开它,同时保持单选按钮的值不变。希望这对你有帮助!