使用ItemsControl类将对象添加到工具箱

时间:2012-06-12 21:45:43

标签: c# .net wpf

我使用两个类作为派生自ItemsControl的Toolbox和派生自ContnentControl的ToolboxItem

// Implements ItemsControl for ToolboxItems    
public class Toolbox : ItemsControl
{

    // Defines the ItemHeight and ItemWidth properties of
    // the WrapPanel used for this Toolbox
    public Size ItemSize
    {
        get { return itemSize; }
        set { itemSize = value; }
    }

    private Size itemSize = new Size(50, 50);

    // Creates or identifies the element that is used to display the given item.        
    protected override DependencyObject GetContainerForItemOverride()
    {
        return new ToolboxItem();
    }

    // Determines if the specified item is (or is eligible to be) its own container.        
    protected override bool IsItemItsOwnContainerOverride(object item)
    {
        return (item is ToolboxItem);
    }
}





// Represents a selectable item in the Toolbox/>.
public class ToolboxItem : ContentControl
{
    // caches the start point of the drag operation
    private Point? dragStartPoint = null;

    static ToolboxItem()
    {
        // set the key to reference the style for this control
        FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(
            typeof(ToolboxItem), new FrameworkPropertyMetadata(typeof(ToolboxItem)));
    }

    protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
    {
        base.OnPreviewMouseDown(e);
        this.dragStartPoint = new Point?(e.GetPosition(this));
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {

        base.OnMouseMove(e);
        if (e.LeftButton != MouseButtonState.Pressed)
            this.dragStartPoint = null;

        if (this.dragStartPoint.HasValue)
        {
            // XamlWriter.Save() has limitations in exactly what is serialized,
            // see SDK documentation; short term solution only;
            string xamlString = XamlWriter.Save(this.Content);
            DragObject dataObject = new DragObject();
            dataObject.Xaml = xamlString;

            WrapPanel panel = VisualTreeHelper.GetParent(this) as WrapPanel;
            if (panel != null)
            {
                // desired size for DesignerCanvas is the stretched Toolbox item size
                double scale = 1.3;
                dataObject.DesiredSize = new Size(panel.ItemWidth * scale, panel.ItemHeight * scale);
            }

            DragDrop.DoDragDrop(this, dataObject, DragDropEffects.Copy);

            e.Handled = true;
        }
    }
}

// Wraps info of the dragged object into a class
public class DragObject
{
    // Xaml string that represents the serialized content
    public String Xaml { get; set; }

    // Defines width and height of the DesignerItem
    // when this DragObject is dropped on the DesignerCanvas
    public Size? DesiredSize { get; set; }
}

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();

    }


    private void Button_Click(object sender, RoutedEventArgs e)
    {

        DesignerItem ni = new DesignerItem();
        ni.AllowDrop = true;
        ni.ToolTip = "tooltip goes here";
        ni.Width = 100;
        ni.Height = 200;
        ni.Background = Brushes.Red;

        //somehow code should introduce the object shape as object. in the sample on codeproject it is reading shape information from xaml but I need to add it from code behind. 


        Toolbox tb = new Toolbox();
        tb.Items.Add(ni);

ItemsControl中的AddChild()方法必须是在工具箱上添加新对象的方法。但是,当我从这个类实例化一个对象时,它不会给我这个方法。为简单起见,我只想在其上添加一个矩形形状,这样我就可以在画布上拖放它。所以问题是如何在toobox上添加一个Rectangle。

感谢任何帮助。

感谢。 阿米特

解决方案:

var TheToolbar = ToolboxContainer.Content as Toolbox;

// Instantiate a ToolboxItem
ToolboxItem TheToolboxItem = new ToolboxItem();

Rectangle myRect = new Rectangle();
myRect.StrokeThickness = 1;
myRect.Stroke = some value for stroke;
myRect.Fill = some value for filling the object;
myRect.IsHitTestVisible = false;
//add to Toolbar
TheToolboxItem.Content= myRect;
TheToolbar.Items.Add(TheToolboxItem);  

1 个答案:

答案 0 :(得分:0)

AddChild()是受保护的方法(=只能从自身或从继承的类访问)。尝试使用这样的代码:

Toolbox tb = new Toolbox();
tb.Items.Add(myNewItem);

实例化对象后,需要将它们作为子元素添加到canvas / parent元素中。

myParentElement.Children.Add(tb);

如果您正在执行此操作,请将其包含在示例代码中。