none
关于WPF装饰器元素点击的问题 RRS feed

  • 问题

  • public class A : Adorner
        {
            public A(UIElement adornedElement)
                : base(adornedElement)
            {
                visualChildren = new VisualCollection(this);
                Button rec = new Button();
                //rec.Background = Brushes.Red;
                rec.Content = "adf";
                rec.Arrange(new Rect(0, 0, 300, 300));
                visualChildren.Add(rec);
    
    
    
            }
    
            protected override int VisualChildrenCount { get { return visualChildren.Count; } }
            protected override Visual GetVisualChild(int index) { return visualChildren[index]; }
            VisualCollection visualChildren;
                
        }
    
    我添加装饰器的时候可以看到 Button 点击Button的时候也能触发事件, 但是没有Button被点击的效果..请问原因..
    


    Everyone Is NO.1
    2011年11月30日 7:37

答案

  • 或者override arrangeoverride 方法:

    public class A : Adorner
    {
        Button rec = new Button();
        public A(UIElement adornedElement)
            : base(adornedElement)
        {
            visualChildren = new VisualCollection(this);
                
            //rec.Background = Brushes.Red;
            rec.Content = "adf";
            rec.Arrange(new Rect(0, 0, 300, 300));
            visualChildren.Add(rec);
        }
        protected override Size ArrangeOverride(Size finalSize)
        {
            double x = 0;
            double y = 0;
            rec.Arrange(new Rect(x, y, 100, 100)); // you need to arrange 
            return finalSize;
        }
        protected override int VisualChildrenCount { get { return visualChildren.Count; } }
        protected override Visual GetVisualChild(int index) { return visualChildren[index]; }
        VisualCollection visualChildren;
    }
    

     

     


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • 已标记为答案 MakubeXSoft 2011年11月30日 8:22
    2011年11月30日 8:20
    版主
  • 第一个方法我给删了
    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • 已标记为答案 MakubeXSoft 2011年11月30日 8:22
    2011年11月30日 8:21
    版主

全部回复

  • 或者override arrangeoverride 方法:

    public class A : Adorner
    {
        Button rec = new Button();
        public A(UIElement adornedElement)
            : base(adornedElement)
        {
            visualChildren = new VisualCollection(this);
                
            //rec.Background = Brushes.Red;
            rec.Content = "adf";
            rec.Arrange(new Rect(0, 0, 300, 300));
            visualChildren.Add(rec);
        }
        protected override Size ArrangeOverride(Size finalSize)
        {
            double x = 0;
            double y = 0;
            rec.Arrange(new Rect(x, y, 100, 100)); // you need to arrange 
            return finalSize;
        }
        protected override int VisualChildrenCount { get { return visualChildren.Count; } }
        protected override Visual GetVisualChild(int index) { return visualChildren[index]; }
        VisualCollection visualChildren;
    }
    

     

     


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • 已标记为答案 MakubeXSoft 2011年11月30日 8:22
    2011年11月30日 8:20
    版主
  • 第一个方法我给删了
    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • 已标记为答案 MakubeXSoft 2011年11月30日 8:22
    2011年11月30日 8:21
    版主
  • 非常感谢...
    Everyone Is NO.1
    2011年11月30日 8:23