当属性绑定到设置奇怪的分裂行为的容器(Strange split container behavio

2019-09-23 04:11发布

我有一个包含两个SplitContainers WinForms应用程序。 第一SplitContainer的跨越整个形式和它的方向是水平的。 第二SplitContainer的是第一分割容器的左侧面板内并且它的方向是垂直的。 我必将每个分割容器的SplitterDistance属性与应用程序设置,使该位置将在用户退出程序进行保存。 然而,这会导致奇怪的问题。 如果我拖动主分割容器的位置,然后将第二裂容器的位置,第一分割容器跳回到原来的位置,该位置不被保存。 如果我删除第二裂容器一切工作正常。 我还没有看到这个问题,如果SplitterDistance属性没有约束。

有谁知道是什么原因造成这样或我怎么能解决这个问题?

更新我现在也想通了,如果我删除属性从第二裂容器一切工作正常结合。 即使我尝试手动实现这个(即,移除属性绑定,并在SplitContainer2.SplitterMoved事件中添加My.Settings.sideDist = SplitContainer2.SplitterDistance),问题仍然存在。

我使用vb.net。

设计器生成的代码:

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.SplitContainer1 = New System.Windows.Forms.SplitContainer()
        Me.SplitContainer2 = New System.Windows.Forms.SplitContainer()
        Me.ListBox1 = New System.Windows.Forms.ListBox()
        Me.ListBox2 = New System.Windows.Forms.ListBox()
        CType(Me.SplitContainer1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SplitContainer1.Panel1.SuspendLayout()
        Me.SplitContainer1.Panel2.SuspendLayout()
        Me.SplitContainer1.SuspendLayout()
        CType(Me.SplitContainer2, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SplitContainer2.Panel1.SuspendLayout()
        Me.SplitContainer2.SuspendLayout()
        Me.SuspendLayout()
        '
        'SplitContainer1
        '
        Me.SplitContainer1.DataBindings.Add(New System.Windows.Forms.Binding("SplitterDistance", Global.WindowsApplication1.My.MySettings.Default, "mainDist", True, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged))
        Me.SplitContainer1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.SplitContainer1.Location = New System.Drawing.Point(0, 0)
        Me.SplitContainer1.Name = "SplitContainer1"
        '
        'SplitContainer1.Panel1
        '
        Me.SplitContainer1.Panel1.Controls.Add(Me.SplitContainer2)
        '
        'SplitContainer1.Panel2
        '
        Me.SplitContainer1.Panel2.Controls.Add(Me.ListBox2)
        Me.SplitContainer1.Size = New System.Drawing.Size(730, 413)
        Me.SplitContainer1.SplitterDistance = Global.WindowsApplication1.My.MySettings.Default.mainDist
        Me.SplitContainer1.TabIndex = 0
        '
        'SplitContainer2
        '
        Me.SplitContainer2.DataBindings.Add(New System.Windows.Forms.Binding("SplitterDistance", Global.WindowsApplication1.My.MySettings.Default, "sideDist", True, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged))
        Me.SplitContainer2.Dock = System.Windows.Forms.DockStyle.Fill
        Me.SplitContainer2.Location = New System.Drawing.Point(0, 0)
        Me.SplitContainer2.Name = "SplitContainer2"
        Me.SplitContainer2.Orientation = System.Windows.Forms.Orientation.Horizontal
        '
        'SplitContainer2.Panel1
        '
        Me.SplitContainer2.Panel1.Controls.Add(Me.ListBox1)
        Me.SplitContainer2.Size = New System.Drawing.Size(310, 413)
        Me.SplitContainer2.SplitterDistance = Global.WindowsApplication1.My.MySettings.Default.sideDist
        Me.SplitContainer2.TabIndex = 0
        '
        'ListBox1
        '
        Me.ListBox1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.ListBox1.FormattingEnabled = True
        Me.ListBox1.Location = New System.Drawing.Point(0, 0)
        Me.ListBox1.Name = "ListBox1"
        Me.ListBox1.Size = New System.Drawing.Size(310, 49)
        Me.ListBox1.TabIndex = 0
        '
        'ListBox2
        '
        Me.ListBox2.Dock = System.Windows.Forms.DockStyle.Fill
        Me.ListBox2.FormattingEnabled = True
        Me.ListBox2.Location = New System.Drawing.Point(0, 0)
        Me.ListBox2.Name = "ListBox2"
        Me.ListBox2.Size = New System.Drawing.Size(416, 413)
        Me.ListBox2.TabIndex = 0
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(730, 413)
        Me.Controls.Add(Me.SplitContainer1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.SplitContainer1.Panel1.ResumeLayout(False)
        Me.SplitContainer1.Panel2.ResumeLayout(False)
        CType(Me.SplitContainer1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.SplitContainer1.ResumeLayout(False)
        Me.SplitContainer2.Panel1.ResumeLayout(False)
        CType(Me.SplitContainer2, System.ComponentModel.ISupportInitialize).EndInit()
        Me.SplitContainer2.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub
    Friend WithEvents SplitContainer1 As System.Windows.Forms.SplitContainer
    Friend WithEvents ListBox2 As System.Windows.Forms.ListBox
    Friend WithEvents SplitContainer2 As System.Windows.Forms.SplitContainer
    Friend WithEvents ListBox1 As System.Windows.Forms.ListBox

End Class

Answer 1:

虽然我建议不要设置DataBindings一个财产SplitContainer通过设计文件(有为什么物业不暴露于设计师的原因),真正的问题是,应用程序设置不会得到更新。 当用户修改SplitterDistance一切正常,但是当控制变为无效时,设置被重新读取并从其中没有从它们的初始值而改变的设置应用。 简单的解决方案是保存新的值重新设定。 下面的代码应该能满足你:

''' <summary>
''' Sets the mainDist application setting to the new SplitterDistance
''' </summary>
''' <remarks>http://stackoverflow.com/users/2659234/brandon-b</remarks>
Private Sub SplitContainer1_SplitterMoved(sender As SplitContainer, e As SplitterEventArgs) Handles SplitContainer1.SplitterMoved
    Global.WindowsApplication1.My.MySettings.Default.mainDist = sender.SplitterDistance
End Sub

''' <summary>
''' Sets the sideDist application setting to the new SplitterDistance
''' </summary>
''' <remarks>http://stackoverflow.com/users/2659234/brandon-b</remarks>
Private Sub SplitContainer2_SplitterMoved(sender As SplitContainer, e As SplitterEventArgs) Handles SplitContainer2.SplitterMoved
    Global.WindowsApplication1.My.MySettings.Default.sideDist = sender.SplitterDistance
End Sub


文章来源: Strange split container behavior when properties are bound to settings