Click here to Skip to main content
15,887,596 members
Home / Discussions / WPF
   

WPF

 
Questionlistviewitem style Pin
caradri13-Mar-16 0:41
caradri13-Mar-16 0:41 
Questionxceed datagrid (community) and virtualization Pin
amigoface9-Mar-16 18:47
amigoface9-Mar-16 18:47 
AnswerRe: window application form work in KIOSK Pin
Pete O'Hanlon8-Feb-16 21:37
mvePete O'Hanlon8-Feb-16 21:37 
AnswerRe: window application form work in KIOSK Pin
Richard Deeming9-Feb-16 1:55
mveRichard Deeming9-Feb-16 1:55 
AnswerRe: window application form work in KIOSK Pin
ZurdoDev9-Feb-16 8:36
professionalZurdoDev9-Feb-16 8:36 
QuestionHow to Programatically click a Button on a navigated Frame Page Pin
Vimalsoft(Pty) Ltd14-Jan-16 3:22
professionalVimalsoft(Pty) Ltd14-Jan-16 3:22 
AnswerRe: How to Programatically click a Button on a navigated Frame Page Pin
Vimalsoft(Pty) Ltd14-Jan-16 5:40
professionalVimalsoft(Pty) Ltd14-Jan-16 5:40 
QuestionCustom FrameworkElement with child controls Pin
Kenneth Haugland12-Jan-16 7:51
mvaKenneth Haugland12-Jan-16 7:51 
I created a standard VisualHost control that just shows a circle at a given location. I combined this with the ability to host a UserControl in the same class, so I could easily switch between a basic edition view that just showed a circle and a more advanced view that showed a UserControl instead.

After a bit I came up with this:
C#
using System;
using System.Windows;
using System.Windows.Media;
using System.ComponentModel;
using System.Windows.Input;

namespace WpfPrismTests
{

    public class CanvasPoint : FrameworkElement, INotifyPropertyChanged
    {

        private VisualCollection _children;

        #region "Constructors"
        public CanvasPoint()
        {
            _children = new VisualCollection(this);
            _children.Add(CreateDrawingVisualCircle());

        }

        protected override void OnMouseEnter(MouseEventArgs e)
        {
            base.OnMouseEnter(e);
        }

        public CanvasPoint(Point p_position)
        {
            PositionOnCanvas = p_position;

            _children = new VisualCollection(this);
            _children.Add(CreateDrawingVisualCircle());
        }
        #endregion

        private DrawingVisual CreateDrawingVisualCircle()
        {
            DrawingVisual drawingVisual = new DrawingVisual();

            // Retrieve the DrawingContext in order to create new drawing content.
            DrawingContext drawingContext = drawingVisual.RenderOpen();

            // Create a circle and draw it in the DrawingContext.
            drawingContext.DrawEllipse(BackgroundColor, new Pen(BackgroundColor, 1.0), PositionOnCanvas, 4, 4);

            // Persist the drawing content.
            drawingContext.Close();

            return drawingVisual;
        }

        #region "Dependency Properties"

        public static DependencyProperty ShowContentProperty = DependencyProperty.Register("ShowContent",
            typeof(bool), typeof(CanvasPoint), new PropertyMetadata(false, OnShowContentPropertyChanged));

        public bool ShowContent
        {
            get
            {
                return (bool)this.GetValue(ShowContentProperty);
            }
            set
            {
                this.SetValue(ShowContentProperty, value);
            }
        }

        private static void OnShowContentPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            bool value = (bool)e.NewValue;
            CanvasPoint thisElement = d as CanvasPoint;
            if (value)
            {
                thisElement.AddLogicalChild(thisElement.Content);
            }
            else
            {
                thisElement.RemoveLogicalChild(thisElement.Content);
            }

        }

        private static FrameworkPropertyMetadata ContentMetadata =
                      new FrameworkPropertyMetadata(null,
                               FrameworkPropertyMetadataOptions.BindsTwoWayByDefault);

        public static readonly DependencyProperty ContentProperty =
            DependencyProperty.RegisterAttached("Content", typeof(UIElement),
                typeof(CanvasPoint), ContentMetadata);

        public UIElement Content
        {
            get
            {
                return (UIElement)this.GetValue(ContentProperty);
            }
            set
            {
                this.SetValue(ContentProperty, value);
            }
        }

        #endregion

        #region "Properties"

        private Point p_PositionOnCanvas = new Point();
        public Point PositionOnCanvas
        {
            get { return p_PositionOnCanvas; }
            set
            {
                p_PositionOnCanvas = value;
                INotifyChange("PositionOnCanvas");
            }
        }

        private SolidColorBrush pBackgoundColor = Brushes.Red;
        public SolidColorBrush BackgroundColor
        {
            get { return pBackgoundColor; }
            set
            {
                pBackgoundColor = value;

                _children.Clear();
                _children = new VisualCollection(this);
                _children.Add(CreateDrawingVisualCircle());
            }
        }
        #endregion

        #region "Overided properties"

        // Provide a required override for the VisualChildrenCount property.
        protected override int VisualChildrenCount
        {

            get
            {
                if (this.ShowContent)
                {
                    UIElement childElement = (UIElement)GetValue(ContentProperty);
                    return childElement != null ? 1 : 0;
                }
                else
                {
                    return _children.Count;
                }
            }
        }

        // Provide a required override for the GetVisualChild method.
        protected override Visual GetVisualChild(int index)
        {
            if (this.ShowContent)
            {
                return (UIElement)GetValue(ContentProperty);
            }
            else
            {
                if (index < 0 || index >= _children.Count)
                {
                    throw new ArgumentOutOfRangeException();
                }

                return _children[index];
            }
        }

        protected override Size MeasureOverride(Size availableSize)
        {
            UIElement childElement = (UIElement)GetValue(ContentProperty);
            if (childElement != null)
                childElement.Measure(availableSize);

            return availableSize;
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            UIElement childElement = (UIElement)GetValue(ContentProperty);
            if (childElement != null)
            {
                Rect ChildSize = new Rect(new Point(0, 0), finalSize);
                double CenterX = ChildSize.Width / 2;
                double CenterY = ChildSize.Height / 2;
                childElement.Arrange(new Rect(new Point(-CenterX + PositionOnCanvas.X, -CenterY + PositionOnCanvas.Y), finalSize));
            }

            return finalSize;
        }


        #endregion

        public event PropertyChangedEventHandler PropertyChanged;

        protected void INotifyChange(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }

    }
}


This works, but I do wonder why the CanvasPoint insisted that the point 0,0 was located in the center of the host control, so I had to adjust the position accordingly:
C#
protected override Size ArrangeOverride(Size finalSize)
{
    UIElement childElement = (UIElement)GetValue(ContentProperty);
    if (childElement != null)
    {
        Rect ChildSize = new Rect(new Point(0, 0), finalSize);
        double CenterX = ChildSize.Width / 2;
        double CenterY = ChildSize.Height / 2;
        childElement.Arrange(new Rect(new Point(-CenterX + PositionOnCanvas.X, -CenterY + PositionOnCanvas.Y), finalSize));
    }

    return finalSize;
}


The second and the real problem is that events in the user control, as Mouse over etc. , won't work with the hosted control, but works with the DrawingContext.Ellipse. What is the problem here?
AnswerRe: Custom FrameworkElement with child controls Pin
Gerry Schmitz12-Jan-16 17:06
mveGerry Schmitz12-Jan-16 17:06 
GeneralRe: Custom FrameworkElement with child controls Pin
Kenneth Haugland13-Jan-16 1:39
mvaKenneth Haugland13-Jan-16 1:39 
GeneralRe: Custom FrameworkElement with child controls Pin
Gerry Schmitz13-Jan-16 6:25
mveGerry Schmitz13-Jan-16 6:25 
QuestionBinding a CompositeCollection to a Canvas (C#) Pin
Kenneth Haugland8-Jan-16 22:31
mvaKenneth Haugland8-Jan-16 22:31 
AnswerRe: Binding a CompositeCollection to a Canvas (C#) Pin
Kenneth Haugland9-Jan-16 2:42
mvaKenneth Haugland9-Jan-16 2:42 
AnswerRe: Binding a CompositeCollection to a Canvas (C#) Pin
Richard Deeming11-Jan-16 3:51
mveRichard Deeming11-Jan-16 3:51 
GeneralRe: Binding a CompositeCollection to a Canvas (C#) Pin
Kenneth Haugland11-Jan-16 4:34
mvaKenneth Haugland11-Jan-16 4:34 
QuestionCan I run UITests on a WPF application. Pin
James_Parsons8-Jan-16 4:56
James_Parsons8-Jan-16 4:56 
AnswerRe: Can I run UITests on a WPF application. Pin
NickPace8-Jan-16 5:06
NickPace8-Jan-16 5:06 
QuestionLabel does not Get Updated Pin
Vimalsoft(Pty) Ltd6-Jan-16 20:33
professionalVimalsoft(Pty) Ltd6-Jan-16 20:33 
AnswerRe: Label does not Get Updated Pin
Mycroft Holmes6-Jan-16 20:45
professionalMycroft Holmes6-Jan-16 20:45 
GeneralRe: Label does not Get Updated Pin
Vimalsoft(Pty) Ltd6-Jan-16 20:49
professionalVimalsoft(Pty) Ltd6-Jan-16 20:49 
GeneralRe: Label does not Get Updated Pin
Mycroft Holmes6-Jan-16 21:04
professionalMycroft Holmes6-Jan-16 21:04 
GeneralRe: Label does not Get Updated Pin
Vimalsoft(Pty) Ltd6-Jan-16 21:09
professionalVimalsoft(Pty) Ltd6-Jan-16 21:09 
QuestionWPF/MVVM Unit Test Questions Pin
Kevin Marois5-Jan-16 5:46
professionalKevin Marois5-Jan-16 5:46 
AnswerRe: WPF/MVVM Unit Test Questions Pin
Pete O'Hanlon5-Jan-16 5:58
mvePete O'Hanlon5-Jan-16 5:58 
GeneralRe: WPF/MVVM Unit Test Questions Pin
Kevin Marois5-Jan-16 6:02
professionalKevin Marois5-Jan-16 6:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.