【问题标题】:adding object to toolbox using ItemsControl class使用 ItemsControl 类将对象添加到工具箱
【发布时间】:2012-06-12 21:45:42
【问题描述】:

我使用两个类作为从 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() 方法必须是在工具箱上添加新对象的方法。但是,当我从这个类实例化一个对象时,它并没有给我这个方法。为简单起见,我只想在其上添加一个矩形,这将允许我将其拖放到画布上。所以问题是我如何在工具箱上添加一个矩形。

感谢任何帮助。

谢谢。 阿米特

解决方案:

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);  

【问题讨论】:

    标签: c# .net wpf


    【解决方案1】:

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

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

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

    myParentElement.Children.Add(tb);
    

    如果您正在这样做,请将其包含在您的示例代码中。

    【讨论】:

    • 是的,我也厌倦了,但是当我调试并观察对象时,我发现某些属性无效。
    • @amitkohan,“无效”是什么意思?
    • 好的,我确实实例化了一个对象,正如您在此处发布的那样。它运行良好,但不会在工具箱上添加/显示任何东西。我想我需要定义一个对象来处理它的呈现。因为这个类使用 XAML 文件作为输入,它从 读取它,在那里你可以看到一组
    • 我想如果我可以声明我在工具箱上添加的对象的形状,它将显示在工具箱上。
    • @amitkohan,您的代码看起来与您提供链接的教程中的代码几乎相同。但是,我看不到您实际将控件添加到画布的代码,也看不到您指定其子项的位置。我怀疑问题出在某个地方;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多