5

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

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

有谁知道这应该如何与 caliburn.micro 一起使用?!

这是xml:

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

1 回答 1

10

我让这个工作没有任何问题,所以我希望我能正确理解你的问题。这是我使用的代码:

视图模型

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>

使用上面的代码,我可以设置单选按钮的值,关闭视图,然后重新打开它,同时保持单选按钮的值。希望有帮助!

于 2013-02-03T18:45:57.803 回答