设置单选按钮使用caliburn micro检查代码

时间:2013-01-15 15:51:30

标签: c# wpf .net-4.0 radio-button caliburn.micro

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

单击并设置值时,它已正确绑定到相应的属性。 当我离开页面并返回时,我需要在代码中设置属性,并期望通过NotifyPropertyChanged在页面上更新它们。 但是不检查RadioButtons,即使相应的属性设置了......

有谁知道这应该如何与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();
    }

1 个答案:

答案 0 :(得分:7)

我没有遇到任何麻烦,所以我希望我能正确理解你的问题。这是我使用的代码:

视图模型

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>

使用上面的代码,我可以设置单选按钮的值,关闭视图,并在单选按钮值保持不变的情况下重新打开它。希望有所帮助!