如何在WPF / C#中创建自定义的MultiSelector / ItemsControl

3
我正在尝试使用C# / WPF创建一个图表应用程序。我的目标有些类似于Microsoft Visio,尽管我并不试图克隆它。我在编码时编写了这个问题,并将我遇到的所有问题都放在了里面,以便有人会发现它有用。也许我想太多了,但我觉得我可以在键盘上吐出更好的代码,所以请随时提出您发现的每个细节的建议(语法除外 :-))。
简而言之: 为什么所有的项目都定位在(0,0)处?
代码:
public class Diagram : MultiSelector
{
    public Diagram()
    {
        this.CanSelectMultipleItems = true;

        // The canvas supports absolute positioning
        FrameworkElementFactory panel = new FrameworkElementFactory(typeof(Canvas)); 
        this.ItemsPanel = new ItemsPanelTemplate(panel);


        // Tells the container where to position the items
        this.ItemContainerStyle = new Style();
        this.ItemContainerStyle.Setters.Add(new Setter(Canvas.LeftProperty, new Binding("X")));
        this.ItemContainerStyle.Setters.Add(new Setter(Canvas.TopProperty, new Binding("Y")));
    }

    protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
    {
        FrameworkElement contentitem = element as FrameworkElement;
        Binding leftBinding = new Binding("X");
        Binding topBinding = new Binding("Y");
        contentitem.SetBinding(Canvas.LeftProperty, leftBinding);
        contentitem.SetBinding(Canvas.TopProperty, topBinding);
        base.PrepareContainerForItemOverride(element, item);
    }

    public class DiagramItem : ContentControl
    {
        private Point _location;

        public DiagramItem()
        {
        }

        static DiagramItem()
        {

        }

        public Point Location
        {
            get { return _location; }
            set
            {
                _location = value;

            }
        }

        public double X
        {
            get { return _location.X; }
            set
            {
                _location.X = value;
            }
        }

        public double Y
        {
            get { return _location.Y; }
            set
            {
                _location.Y = value;
            }
        }
    }

//...

好的,这个想法是 Diagram : ItemsControl 将其项目放置在画布面板上,在项目 DiagramItem.Location 中定义的位置。也就是说,当我更改 DiagramItem 中的 X 属性时,Diagram 会在 x 轴上移动该项。

注意:MultiSelector 派生自 ItemsControl 和 Selector,仅在此处使用,因为我需要选择显示的项目。


请注意,如果可能的话我不想使用XAML。

详细说明:
用户所看到的Diagram实例需要满足以下要求:

  1. 具有多个DiagramItems。
  2. 用户可以选择多个DiagramItems。
  3. DiagramItems可以被调整大小、旋转并拖动到Diagram上的任何位置。
  4. 可以使用键盘在DiagramItems之间导航。

基本上,我有两个或可能三个与此问题相关的类。

  • Diagram扩展了System.Windows.Controls.Primitives.MultiSelector:Selector:ItemsControl
  • DiagramItem扩展了ContentControl或其他控件

Diagram.ItemsPanel,即显示项目的可视面板,应该是支持绝对定位的面板,例如Canvas

我应该如何实现从MultiSelector派生的类,以及您可以指向哪些与此问题相关的资源?

在实现自定义MultiSelector / ItemsControl时,需要考虑什么?


资源:

我发现与我的问题相关的资源非常少,但是我不确定应该寻找什么。我已经使用Reflector阅读了ListBox和ListBoxItem的源代码,但并没有发现它很有用。

其他资源:

2个回答

6

好的,显然可以通过使用绑定和属性框架轻松实现此功能。

    public class Diagram : MultiSelector
    {
        public Diagram()
        {
           this.CanSelectMultipleItems = true;


            // The canvas supports absolute positioning
            FrameworkElementFactory panel = new FrameworkElementFactory(typeof(Canvas)); 
            this.ItemsPanel = new ItemsPanelTemplate(panel);


            // Tells the container where to position the items
           this.ItemContainerStyle = new Style();
            this.ItemContainerStyle.Setters.Add(new Setter(Canvas.LeftProperty, new Binding("X")));
            this.ItemContainerStyle.Setters.Add(new Setter(Canvas.TopProperty, new Binding("Y")));
        }


        protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
        {
            FrameworkElement contentitem = element as FrameworkElement;

            Binding leftBinding = new Binding("XProperty");
            leftBinding.Source = contentitem;

            Binding topBinding = new Binding("YProperty");
            topBinding.Source = contentitem;

            contentitem.SetBinding(Canvas.LeftProperty, leftBinding);
            contentitem.SetBinding(Canvas.TopProperty, topBinding);
            base.PrepareContainerForItemOverride(element, item);
        }





 public class DiagramItem : ContentControl
 {
       public static readonly DependencyProperty XProperty;
       public static readonly DependencyProperty YProperty;
       public static readonly RoutedEvent SelectedEvent;
       public static readonly RoutedEvent UnselectedEvent;
       public static readonly DependencyProperty IsSelectedProperty;

       public DiagramItem()
       {
       }

       static DiagramItem()
       {
            XProperty = DependencyProperty.Register("XProperty", typeof(Double), typeof(DiagramItem));
            YProperty = DependencyProperty.Register("YProperty", typeof(Double), typeof(DiagramItem));
            SelectedEvent = MultiSelector.SelectedEvent.AddOwner(typeof(DiagramItem));
            UnselectedEvent = MultiSelector.SelectedEvent.AddOwner(typeof(DiagramItem));
            IsSelectedProperty = MultiSelector.IsSelectedProperty.AddOwner(typeof(DiagramItem));

       }


       public Double X
       {
            get
            {
                return (Double)this.GetValue(XProperty);
            }

            set
            {
                this.SetValue(XProperty, value);
            }
       }

       public Double Y
       {
            get
            {
                return (Double)this.GetValue(YProperty);
            }
            set
            {
                 this.SetValue(YProperty, value);
            }
        }
    
        public Point Location
        {
            get
            {
                return new Point(X, Y);
            }

            set
            {
                this.X = value.X;
                this.Y = value.Y;
            }
        }

    }

魔法在于正确使用绑定,关键是将contentitem添加为Source。下一步显然是处理项目的选择,但这是另一个问题。


这行代码 UnselectedEvent = MultiSelector.SelectedEvent.AddOwner 是否应该改为 UnselectedEvent = MultiSelector.UNSelectedEvent.AddOwner - StayOnTarget
我很好奇你是如何处理选择的,你最终解决了吗? - Piotr Golacki

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接