为什么不能将Windows窗体的大小绑定到ApplicationSettings? [英] Why can't you bind the Size of a windows form to ApplicationSettings?

查看:147
本文介绍了为什么不能将Windows窗体的大小绑定到ApplicationSettings?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到它的工作,看到我的答案下面的代码...

正如Tundey在最后一个问题,您可以很轻松地将几乎所有关于Windows窗体控件的内容绑定到ApplicationSettings。那么真的没有办法做这样的形式大小? 本教程表示您需要明确处理大小,以便您可以保存RestoreBounds而不是如果窗口最大化或最小化,则大小。但是,我希望我可以使用如下的资源:

As Tundey pointed out in his answer to my last question, you can bind nearly everything about a windows forms control to ApplicationSettings pretty effortlessly. So is there really no way to do this with form Size? This tutorial says you need to handle Size explicitly so you can save RestoreBounds instead of size if the window is maximized or minimized. However, I hoped I could just use a property like:

public Size RestoreSize
{
    get
    {
        if (this.WindowState == FormWindowState.Normal)
        {
            return this.Size;
        }
        else
        {
            return this.RestoreBounds.Size;
        }
    }
    set
    {
        ...
    }
}

但是我看不到在设计器中绑定这个方法的方法(PropertyBinding列表中的Size大大缺少)

But I can't see a way to bind this in the designer (Size is notably missing from the PropertyBinding list).

推荐答案

我终于想出了一个Form子类,一劳永逸地解决了这个问题。使用它:

I finally came up with a Form subclass that solves this, once and for all. To use it:


  1. 从RestorableForm而不是Form继承。

  2. 在(ApplicationSettings) ) - >(PropertyBinding)到WindowRestoreState。

  3. 当窗口即将关闭时调用Properties.Settings.Default.Save()。

现在,窗口位置和状态将在会话之间被记住。根据下面其他海报的建议,我附带了一个ConstrainToScreen函数,确保窗口恢复时可用的显示窗口很好。

Now window position and state will be remembered between sessions. Following the suggestions from other posters below, I included a function ConstrainToScreen that makes sure the window fits nicely on the available displays when restoring itself.

// Consider this code public domain. If you want, you can even tell
// your boss, attractive women, or the other guy in your cube that
// you wrote it. Enjoy!

using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;

namespace Utilities
{
    public class RestorableForm : Form, INotifyPropertyChanged
    {
        // We invoke this event when the binding needs to be updated.
        public event PropertyChangedEventHandler PropertyChanged;

        // This stores the last window position and state
        private WindowRestoreStateInfo windowRestoreState;

        // Now we define the property that we will bind to our settings.
        [Browsable(false)]        // Don't show it in the Properties list
        [SettingsBindable(true)]  // But do enable binding to settings
        public WindowRestoreStateInfo WindowRestoreState
        {
            get { return windowRestoreState; }
            set
            {
                windowRestoreState = value;
                if (PropertyChanged != null)
                {
                    // If anybody's listening, let them know the
                    // binding needs to be updated:
                    PropertyChanged(this,
                        new PropertyChangedEventArgs("WindowRestoreState"));
                }
            }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            WindowRestoreState = new WindowRestoreStateInfo();
            WindowRestoreState.Bounds
                = WindowState == FormWindowState.Normal ?
                  Bounds : RestoreBounds;
            WindowRestoreState.WindowState = WindowState;

            base.OnClosing(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (WindowRestoreState != null)
            {
                Bounds = ConstrainToScreen(WindowRestoreState.Bounds);
                WindowState = WindowRestoreState.WindowState;
            }
        }

        // This helper class stores both position and state.
        // That way, we only have to set one binding.
        public class WindowRestoreStateInfo
        {
            Rectangle bounds;
            public Rectangle Bounds
            {
                get { return bounds; }
                set { bounds = value; }
            }

            FormWindowState windowState;
            public FormWindowState WindowState
            {
                get { return windowState; }
                set { windowState = value; }
            }
        }

        private Rectangle ConstrainToScreen(Rectangle bounds)
        {
            Screen screen = Screen.FromRectangle(WindowRestoreState.Bounds);
            Rectangle workingArea = screen.WorkingArea;

            int width = Math.Min(bounds.Width, workingArea.Width);
            int height = Math.Min(bounds.Height, workingArea.Height);

            // mmm....minimax
            int left = Math.Min(workingArea.Right - width,
                                Math.Max(bounds.Left, workingArea.Left));
            int top = Math.Min(workingArea.Bottom - height,
                                Math.Max(bounds.Top, workingArea.Top));

            return new Rectangle(left, top, width, height);
        }
    }
}



设置绑定参考




  • SettingsBindableAttribute

  • INotifyPropertyChanged

  • Settings Bindings References

    • SettingsBindableAttribute
    • INotifyPropertyChanged
    • 这篇关于为什么不能将Windows窗体的大小绑定到ApplicationSettings?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆