2

我正在开发一个程序来管理具有本地 UI 和远程界面的 minecraft 服务器。我在功能区栏上有一个按钮,可以启用或禁用远程接口和一个用于输入端口的文本框。目前,我在启用网络时禁用了文本框,但是,在我再次将其设置为 true 后,禁用并不会重新启用文本框(并且设置断点显示它仍然为 false)。

    private void NetToggleChecked(object sender, RoutedEventArgs e) {
        portTextBox.IsEnabled = false;
        if (ButtonPressedByUser) {
            var result = MessageBox.Show("Are you sure you want to enable networking with the current settings?" +
                                         " If not properly configured, it may be possible for an attacker to enter your server.",
                                         "Simple Bukkit Wrapper", MessageBoxButton.YesNo, MessageBoxImage.Warning,
                                         MessageBoxResult.No);
            if (result == MessageBoxResult.No) {
                ButtonPressedByUser = false;
                NetworkToggle.IsChecked = false;
                ButtonPressedByUser = true;
                return;
            }
        }

        Config.NetConf["enabled"] = "true";

        int port;
        if (!int.TryParse(Config.NetConf["port"], out port)) {
            MessageBox.Show("Port could not be parsed (is it a number?)");
            ButtonPressedByUser = false;
            NetworkToggle.IsChecked = false;
            ButtonPressedByUser = true;
            return;
        }

        Net.Listener.StartListening(port);
    }

    private void NetworkToggleUnchecked(object sender, RoutedEventArgs e) {
        portTextBox.IsEnabled = true;
        if (ButtonPressedByUser) {
            var result =
                MessageBox.Show("Are you sure you wish to disable all networking to your server? It will " +
                                "be impossible to connect to it remotely and any existing connections will be closed.",
                                "", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No);
            if (result == MessageBoxResult.No) {
                ButtonPressedByUser = false;
                NetworkToggle.IsChecked = true;
                ButtonPressedByUser = true;
                return;
            }
        }

        Config.NetConf["enabled"] = "false";
        Net.Listener.StopListening();
    }

感谢您帮助解决文本框无法再次启用的原因。

4

1 回答 1

7

老问题,但我在寻找答案时一直遇到它,所以我想无论如何我都会发布答案。如果没有关联的命令,ribbonTextbox 控件中存在导致 isenabled 始终为 false 的错误。根据我的发现,有两种方法可以解决这个问题:

1:基于ribbontextbox新建控件,覆盖isenabledcore属性,始终返回true。如此处所示不能将 RibbonTextBox isEnable 设置为 False

2:创建一个虚拟命令并将其与控件关联

public static readonly ICommand DummyCommand = new RoutedCommand("Dummy", typeof(Control));
    public static void Dummy(Object sender, ExecutedRoutedEventArgs e)
    {
        // Do nothing its a dummy command
    }
    public static void CanDummy(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = true;
    } 

如此链接http://blogs.msdn.com/b/wpf/archive/2010/10/21/wpf-ribbon-october-2010-update-details.aspx中的评论所述。正如我所说的可能对原始海报没有帮助,但我在寻找答案时不断遇到它,因此它可以为其他人节省几分钟的谷歌搜索时间。

于 2012-08-07T20:56:44.303 回答