2017-04-07 7 views
-1

コンテナ内の領域を描画したい。これは私がカスタムコントロールを使用することについて考えた。コンテナ内のWPF描画境界

私の問題は、キャンバスが与えられたすべてのスペースを占めていないということです。それを強制的にどのようにすべての利用可能なスペースを使用

何私が行っていることはキャンバスから継承され、その内側境界要素が作成されます。

public class DrawableCanvas : Canvas 
{ 
    private Border border; 

    static DrawableCanvas() 
    { 
     DefaultStyleKeyProperty.OverrideMetadata(typeof(DrawableCanvas), new FrameworkPropertyMetadata(typeof(DrawableCanvas))); 
    } 

    public DrawableCanvas() 
    { 
     this.border = new Border(); 
     border.Background = new SolidColorBrush(Colors.Blue); 
     border.BorderThickness = new Thickness(1); 
     border.Width = 0; 
     border.Visibility = Visibility.Hidden; 
     border.Opacity = 0.3; 

     this.Children.Add(border); 

     this.MouseLeftButtonDown += DrawableCanvas_MouseLeftButtonDown; 
     this.MouseLeftButtonUp += DrawableCanvas_MouseLeftButtonUp; 
     this.MouseMove += DrawableCanvas_MouseMove; 
    } 

    private void DrawableCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) 
    { 
     Debug.WriteLine("Left mouse up"); 

     // release mouse 
     Mouse.Capture(null); 

     border.Width = 0; 
     border.Height = 100; 

     startPosition = 0; 
    } 

    double startPosition = 0; 
    private void DrawableCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
    { 
     Debug.WriteLine("Left mouse down"); 

     border.Visibility = Visibility.Visible; 

     // capture mouse 
     Mouse.Capture(this); 

     var point = e.GetPosition(this); 
     startPosition = point.X; 

     SetLeft(border, point.X); 
    } 

    private void DrawableCanvas_MouseMove(object sender, MouseEventArgs e) 
    { 
     if(this.IsMouseCaptured) 
     { 
      var point = e.GetPosition(this); 

      Debug.WriteLine("Mouse move"); 

      // set the position to far left 
      SetLeft(border, Math.Min(startPosition, point.X)); 

      // width is the difference between two points 
      border.Width = Math.Abs(startPosition - point.X); 

      Debug.WriteLine(Math.Min(startPosition, point.X)); 
      Debug.WriteLine(border.Width); 
     } 
    } 
} 

とビューのために:

<DockPanel> 
    <local:DrawableCanvas> 
     <Rectangle Height="500" Width="500" Fill="Transparent" /> 
    </local:DrawableCanvas> 
</DockPanel> 

私はこのような何かしたい: enter image description here

+0

DockPanelがすべてのスペースに伸びていますか?より多くのXAMLを提供します。 –

+1

私はあなたのコードをDockPanelでウィンドウ内のグリッド内で試しました。私が行った唯一の変更は ''でした。 'DockPanel'は' Grid'を満たし、 'Grid'はウィンドウをいっぱいにします。なぜあなたに問題があると思うのか説明してください。 'Border'の高さは常に100であるのであなたは混乱していますか?あなたはそれをそれに設定し、決してそれを変更しないからです。 –

+0

矩形要素を削除してみます。マウスイベントはこの場合トリガされません。 キャンバスの背景を透明に設定したとき、私はこのことが分かりました。なぜ私はあなたの答えを受け入れるか説明することができます:) –

答えて

0

何を私は、このコントロールを作成してuiで選択できるようにしたかったのです。このため

iは幅のみにまたがり、それを取得、すべての高さを使用する境界線/四角形を必要と私はこれで終わった

ビュー:

<Window x:Class="Wpf.Test01.Stack_43281567" 
     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" 
     xmlns:local="clr-namespace:Wpf.Test01.Controls" 
     mc:Ignorable="d" 
     Title="Stack_43281567" Height="300" Width="300"> 
    <Grid> 
     <local:DrawableCanvas /> 
    </Grid> 
</Window> 

DrawableCanvas.cs:

public class DrawableCanvas : Canvas 
{ 
    private Border border; 

    static DrawableCanvas() 
    { 
     DefaultStyleKeyProperty.OverrideMetadata(typeof(DrawableCanvas), new FrameworkPropertyMetadata(typeof(DrawableCanvas))); 
    } 

    // when selection is done expose event to notify the user of selection change 

    public DrawableCanvas() 
    { 
     this.border = new Border(); 
     border.Background = new SolidColorBrush(Colors.Blue); 
     border.BorderThickness = new Thickness(1); 
     border.Visibility = Visibility.Hidden; 
     border.Opacity = 0.3; 
     border.Height = this.ActualHeight; 

     this.Children.Add(border); 
     this.Background = new SolidColorBrush(Colors.Transparent); 

     this.MouseLeftButtonDown += DrawableCanvas_MouseLeftButtonDown; 
     this.MouseLeftButtonUp += DrawableCanvas_MouseLeftButtonUp; 
     this.MouseMove += DrawableCanvas_MouseMove; 

     this.SizeChanged += DrawableCanvas_SizeChanged; 
    } 

    private void DrawableCanvas_SizeChanged(object sender, SizeChangedEventArgs e) 
    { 
     border.Height = e.NewSize.Height; 
    } 

    private void DrawableCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) 
    { 
     Debug.WriteLine("Left mouse up"); 

     // release mouse 
     Mouse.Capture(null); 

     border.Width = 0; 

     startPosition = 0; 
    } 

    double startPosition = 0; 
    private void DrawableCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
    { 
     Debug.WriteLine("Left mouse down"); 

     border.Visibility = Visibility.Visible; 

     // capture mouse 
     Mouse.Capture(this); 

     var point = e.GetPosition(this); 
     startPosition = point.X; 

     SetLeft(border, point.X); 
    } 

    private void DrawableCanvas_MouseMove(object sender, MouseEventArgs e) 
    { 
     if (this.IsMouseCaptured) 
     { 
      var point = e.GetPosition(this); 

      Debug.WriteLine("Mouse move"); 

      // set the position to far left 
      SetLeft(border, Math.Min(startPosition, point.X)); 

      // width is the difference between two points 
      border.Width = Math.Abs(startPosition - point.X); 

      Debug.WriteLine(Math.Min(startPosition, point.X)); 
      Debug.WriteLine(border.Width); 
     } 
    } 
} 
関連する問題