I'm having problems with getting my content to size correctly. I'm
trying to implement what are effectively 'popup' windows using WPF. The
These windows are implemented with a grid which contains a canvas. The
canvases size is variable and MeasureOverride is implemented to return
the size of the canvas.
One this which complicates this is that the owner canvas of the popups
implements it's own VisualCollection so items are manually added to the
VisualCollection.
I can't get the popup window grid to resize so it's content is visible.
The only way I can get the popup to display at all is by calling
popup.Arrange(). The width and height passed into Arrange are basically
dummy values but they seem to be sticking.
Any help would be appreciated-
John
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Input;
namespace scroll
{
public class content_t : Canvas
{
public content_t()
{
Background = Brushes.LightBlue;
}
// this returns the requested size of the content
protected override Size MeasureOverride( Size sz ) {
return new Size( 200, 200 );
}
}
public class popup_t : Grid
{
public popup_t(String title)
{
this.ClipToBounds = true;
Background = Brushes.Transparent;
// one column
ColumnDefinitions.Add( new ColumnDefinition() );
// 2 rows
RowDefinitions.Add( new RowDefinition() );
RowDefinitions.Add( new RowDefinition() );
RowDefinitions[0].Height = new GridLength(18);
// background
System.Windows.Shapes.Rectangle rc = new
System.Windows.Shapes.Rectangle();
rc.Fill = Brushes.Gray;
rc.Stroke = Brushes.Gray;
rc.RadiusX = 3;
rc.RadiusY = 3;
SetColumnSpan( rc, 2 );
SetRowSpan( rc, 2 );
Children.Add( rc );
// title bar
TextBlock tb = new TextBlock();
tb.Text = title;
tb.Foreground = Brushes.DarkGray;
tb.VerticalAlignment = System.Windows.VerticalAlignment.Center;
tb.Margin = new Thickness( 3, 3, 3, 0 );
SetRow( tb, 0 );
SetColumn( tb, 0 );
Children.Add( tb );
// create content
content_t content = new content_t();
SetRow(content, 1);
SetColumn(content, 0);
Children.Add(content);
content.Margin = new Thickness(3, 0, 3, 3);
}
}
public class canvas_t : Canvas
{
private VisualCollection _children;
public canvas_t()
{
_children = new VisualCollection(this);
Background = Brushes.AntiqueWhite;
}
protected override void OnMouseDown(MouseButtonEventArgs e)
{
// create a window
Point pt = e.GetPosition( this );
popup_t popup = new popup_t("title");
_children.Add(popup);
popup.Arrange(new Rect(pt.X, pt.Y, 100, 100 ));
}
protected override int VisualChildrenCount
{
get { return _children.Count; }
}
// Provide a required override for the GetVisualChild method.
protected override Visual GetVisualChild(int index)
{
if (index < 0 || index > _children.Count)
{
throw new ArgumentOutOfRangeException();
}
return (Visual)_children[index];
}
}
public class app_t : Application
{
Window win;
protected override void OnStartup(StartupEventArgs e)
{
win = new Window();
win.Title = "popup";
win.Content = new canvas_t();
win.Show();
base.OnStartup(e);
}
}
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
app_t app = new app_t();
app.Run();
}
}
}


