2013-02-05 7 views
0

WinForms를 사용하여 실제 프로그램에서 문제가 발생하면 최소한의 테스트 응용 프로그램을 만들고 있습니다. 나는 더 큰 패널 (부모) 안에 작은 패널 (자식)을 넣었다. 더 큰 패널에는 AutoScroll이 true로 설정됩니다. 하위 패널에는 기본 앵커가 위쪽 및 왼쪽으로 설정되어 있습니다. 하위 패널이 고정되어 있지 않습니다.패널의 패널, 자동 스크롤

원하는 동작은 작은 패널의 위치가 위쪽, 아래쪽, 왼쪽 또는 오른쪽으로 너무 오프셋 될 때마다 스크롤 막대가 표시되도록하는 것입니다. 문제는 그것이 너무 멀리 떨어져 있거나 너무 멀리있을 때만 작동한다는 것입니다. 스크롤바가 너무 많거나 왼쪽 방향으로 너무 많이있을 때는 스크롤바가 나타나지 않습니다.

두 개의 간단한 버튼을 사용하여 하위 패널의 위치를 ​​왼쪽으로 200 픽셀, 오른쪽으로 200 픽셀로 설정하면 위치를 쉽게 수정할 수 있습니다. 빨리 WPF 해결 Another 윈폼 불능 -

 private void InitializeComponent() 
    { 
     this.hostPanel = new System.Windows.Forms.Panel(); 
     this.childPanel = new System.Windows.Forms.Panel(); 
     this.moveChildLeft = new System.Windows.Forms.Button(); 
     this.moveChildRight = new System.Windows.Forms.Button(); 
     this.hostPanel.SuspendLayout(); 
     this.SuspendLayout(); 
     // 
     // hostPanel 
     // 
     this.hostPanel.AutoScroll = true; 
     this.hostPanel.BackColor = System.Drawing.SystemColors.AppWorkspace; 
     this.hostPanel.Controls.Add(this.childPanel); 
     this.hostPanel.Location = new System.Drawing.Point(239, 48); 
     this.hostPanel.Name = "hostPanel"; 
     this.hostPanel.Size = new System.Drawing.Size(400, 400); 
     this.hostPanel.TabIndex = 0; 
     // 
     // childPanel 
     // 
     this.childPanel.BackColor = System.Drawing.SystemColors.ButtonHighlight; 
     this.childPanel.Location = new System.Drawing.Point(29, 62); 
     this.childPanel.Name = "childPanel"; 
     this.childPanel.Size = new System.Drawing.Size(342, 259); 
     this.childPanel.TabIndex = 0; 
     // 
     // moveChildLeft 
     // 
     this.moveChildLeft.Location = new System.Drawing.Point(61, 81); 
     this.moveChildLeft.Name = "moveChildLeft"; 
     this.moveChildLeft.Size = new System.Drawing.Size(75, 23); 
     this.moveChildLeft.TabIndex = 1; 
     this.moveChildLeft.Text = "Left 200"; 
     this.moveChildLeft.UseVisualStyleBackColor = true; 
     this.moveChildLeft.Click += new System.EventHandler(this.button1_Click); 
     // 
     // moveChildRight 
     // 
     this.moveChildRight.Location = new System.Drawing.Point(61, 111); 
     this.moveChildRight.Name = "moveChildRight"; 
     this.moveChildRight.Size = new System.Drawing.Size(75, 23); 
     this.moveChildRight.TabIndex = 2; 
     this.moveChildRight.Text = "Right 200"; 
     this.moveChildRight.UseVisualStyleBackColor = true; 
     this.moveChildRight.Click += new System.EventHandler(this.button2_Click); 
     // 
     // Form1 
     // 
     this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
     this.ClientSize = new System.Drawing.Size(1018, 549); 
     this.Controls.Add(this.moveChildRight); 
     this.Controls.Add(this.moveChildLeft); 
     this.Controls.Add(this.hostPanel); 
     this.Name = "Form1"; 
     this.Text = "Form1"; 
     this.hostPanel.ResumeLayout(false); 
     this.ResumeLayout(false); 
    } 
+0

고정되어있는 경우 어떻게 위쪽이나 왼쪽으로 지나치게 많을 수 있습니까? –

+0

하위 패널의 앵커 속성을 제거하면 (즉, none으로 설정 한 경우) 하위 패널의 위치에 관계없이 호스트 패널에 스크롤 막대가 전혀 표시되지 않습니다. 자동 스크롤 막대는 어딘가에 적어도 1 개의 앵커가 있는지에 따라 달라집니다. –

답변

0

Yet :

XAML :

<Window x:Class="WpfApplication4.Window3" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="Window3" WindowState="Maximized"> 
    <DockPanel> 
     <StackPanel DockPanel.Dock="Top" Orientation="Horizontal"> 
      <Button Content="Left" Click="MoveLeft"/> 
      <Button Content="Right" Click="MoveRight"/> 
     </StackPanel> 
     <Border BorderBrush="Blue" BorderThickness="1" Width="300" Height="300"> 
      <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" x:Name="Scr"> 
       <Grid Background="Green" Width="100" Height="100" x:Name="Grid"/> 
      </ScrollViewer> 
     </Border> 
    </DockPanel> 
</Window> 
다음
 private void button1_Click(object sender, EventArgs e) 
    { 
     childPanel.Location = new Point(childPanel.Location.X - 200, childPanel.Location.Y); 
     hostPanel.Invalidate(); 
    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     childPanel.Location = new Point(childPanel.Location.X + 200, childPanel.Location.Y); 
     hostPanel.Invalidate(); 
    } 

디자이너 코드입니다 :

은 여기 내를 Form1() 코드입니다 ,

뒤에 코드 :

using System.Windows; 

namespace WpfApplication4 
{ 
    public partial class Window3 : Window 
    { 
     public Window3() 
     { 
      InitializeComponent(); 
     } 

     private void MoveRight(object sender, RoutedEventArgs e) 
     { 
      if (Grid.Margin.Right <= 0) 
      { 
       Grid.Margin = new Thickness(Grid.Margin.Left + 100,0,0,0); 
      } 
      else 
      { 
       Grid.Margin = new Thickness(0, 0, Grid.Margin.Right - 100, 0); 
       Scr.ScrollToHorizontalOffset(Scr.HorizontalOffset - 100); 
      } 
     } 

     private void MoveLeft(object sender, RoutedEventArgs e) 
     { 
      if (Grid.Margin.Left > 0) 
      { 
       Grid.Margin = new Thickness(Grid.Margin.Left - 100, 0, 0, 0); 
      } 
      else 
      { 
       Grid.Margin = new Thickness(0, 0, Grid.Margin.Right + 100, 0); 
       Scr.ScrollToHorizontalOffset(Scr.HorizontalOffset + 100); 
      } 
     } 
    } 
} 

복사하여 파일에 내 코드를 붙여 -> 새로 만들기 -> WPF 응용 프로그램 및 자신에 대한 결과를 참조하십시오.

0

결국 앱을 WPF로 변환 할 수 있습니다. 이후 Winform은 작은 죽음으로 정죄받습니다.

관련 문제