gpt4 book ai didi

c# - 使用 caliburn micro 设置 RadioButton Checked in code

转载 作者:太空狗 更新时间:2023-10-30 00:32:58 28 4
gpt4 key购买 nike

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

单击并设置值时,它会正确绑定(bind)到相应的属性。当我离开页面并返回时,我需要在代码中设置属性并期望它们通过 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" />

这里是我的 View 模型中的代码:

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

最佳答案

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

View 模型

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

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>

使用上面的代码,我能够设置单选按钮的值,关闭 View ,然后在保持单选按钮值不变的情况下重新打开它。希望对您有所帮助!

关于c# - 使用 caliburn micro 设置 RadioButton Checked in code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14341484/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com