2017-04-07 2 views
-1

Ich möchte Bereich innerhalb eines Containers zeichnen. Hierfür habe ich über die Verwendung von benutzerdefinierten Steuerelementen nachgedacht.WPF ziehen Grenze in Container

Mein Problem ist, dass Canvas nicht den gesamten Platz beansprucht, den es bekommt. Wie erzwingen Sie es verwenden Sie alle verfügbaren Speicherplatz?

Was ich habe, ist getan von Canvas geerbt und im Inneren Randelement erstellt:

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); 
     } 
    } 
} 

Und für Ansicht:

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

Ich möchte etwas wie folgt aus: enter image description here

+0

Sind Sie sicher, dass sich das DockPanel auf den gesamten Platz erstreckt? Stellen Sie mehr XAML zur Verfügung. –

+1

Ich habe Ihren Code mit dem DockPanel in einem Grid in einem Fenster versucht. Die einzige Änderung, die ich gemacht habe, war ''. Das 'DockPanel' füllt das' Grid' und das 'Grid' füllt das Fenster. Bitte erklären Sie, warum Sie denken, dass Sie ein Problem haben. Sind Sie verwirrt, weil die 'Border'-Höhe immer 100 ist? Das liegt daran, dass Sie es darauf einstellen und es niemals ändern. –

+0

Versuchen Sie, das Rechteckelement zu entfernen. Mausereignisse werden in diesem Fall nicht ausgelöst. Ich habe gerade herausgefunden, wenn ich Hintergrund der Leinwand zu transparent, dies funktioniert. Können Sie mir erklären, warum ich dann Ihre Antwort akzeptiere :) –

Antwort

0

Was Ich wollte dieses Steuerelement erstellen, um die Auswahl auf ui zu ermöglichen.

Dazu brauchte ich eine Grenze/Rechteck, das nur in der Breite überspannen wird und verbrauchen alle die Höhe wird es

ich damit endete:

Ausblick:

<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); 
     } 
    } 
} 
Verwandte Themen