指定的元素已经是另一个元素的逻辑子元素。首先断开它。在用户控制中

时间:2016-03-18 19:16:32

标签: c# wpf xaml wpf-controls

我有这个UserControl:

[ContentProperty("Items")]
[DefaultProperty("Items")]
public partial class PanelControl : UserControl
{
    public static readonly DependencyProperty OrientationProperty = DependencyProperty.Register("Orientation", typeof(Orientation), typeof(PanelControl), new FrameworkPropertyMetadata(Orientation.Horizontal, new PropertyChangedCallback(OnOrientationChanged)));
    public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register("Items", typeof(ObservableCollection<UIElement>), typeof(PanelControl), new FrameworkPropertyMetadata(new ObservableCollection<UIElement>(), new PropertyChangedCallback(OnItemsChanged)));
    public static readonly DependencyProperty SizeProperty = DependencyProperty.RegisterAttached("Size", typeof(double), typeof(PanelControl), new FrameworkPropertyMetadata(1.0, new PropertyChangedCallback(OnSizeChanged)), new ValidateValueCallback(IsSizeValid));

    public Orientation Orientation
    {
        get
        {
            return (Orientation)GetValue(OrientationProperty);
        }
        set
        {
            SetValue(OrientationProperty, value);
        }
    }

    public ObservableCollection<UIElement> Items
    {
        get
        {
            return (ObservableCollection<UIElement>)GetValue(ItemsProperty);
        }
        set
        {
            SetValue(ItemsProperty, value);
        }
    }

    public static void SetSize(UIElement element, double size)
    {
        element.SetValue(SizeProperty, size);
    }
    public static double GetSize(UIElement element)
    {
        return (double)element.GetValue(SizeProperty);
    }

    private static void OnOrientationChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
    {
        /*MessageBox.Show("orientation");*/
        ((PanelControl)dependencyObject).ClearAndBuildGrid();
    }

    private static void OnItemsChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
    {
        /*MessageBox.Show("items");*/
        ((PanelControl)dependencyObject).ClearAndBuildGrid();
        if(args.OldValue != null)
            ((ObservableCollection<UIElement>)args.OldValue).CollectionChanged -= ((PanelControl)dependencyObject).OnItemsCollectionChanged;
        if (args.NewValue != null)
            ((ObservableCollection<UIElement>)args.NewValue).CollectionChanged += ((PanelControl)dependencyObject).OnItemsCollectionChanged;

    }

    private void OnItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs args)
    {
        /*MessageBox.Show("collection");*/
        ClearAndBuildGrid();
    }

    private static void OnSizeChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
    {
        ((PanelControl)dependencyObject).ClearAndBuildGrid();
        /*MessageBox.Show("size");*/
    }
    private static bool IsSizeValid(object value)
    {
        return (double)value < 0 ? false : true;
    }

    private void ClearAndBuildGrid()
    {
        MainGrid.Children.Clear();
        MainGrid.RowDefinitions.Clear();
        MainGrid.ColumnDefinitions.Clear();
        /*MessageBox.Show(MainGrid.Children.Count.ToString());*/
        for (int i = 0; i < Items.Count; i++)
        {
            if (Orientation == Orientation.Horizontal)
            {
                MainGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = SizeToGridLength(GetSize(Items[i])) });
                Grid.SetColumn(Items[i], i * 2);
                if (i != Items.Count - 1)
                {
                    MainGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(5) });
                    GridSplitter splitter = new GridSplitter() { ResizeDirection = GridResizeDirection.Columns, HorizontalAlignment = HorizontalAlignment.Stretch };
                    Grid.SetColumn(splitter, i * 2 + 1);
                    MainGrid.Children.Add(splitter);
                }
            }
            else
            {
                MainGrid.RowDefinitions.Add(new RowDefinition() { Height = SizeToGridLength(GetSize(Items[i])) });
                Grid.SetRow(Items[i], i * 2);
                if (i != Items.Count - 1)
                {
                    MainGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(5) });
                    GridSplitter splitter = new GridSplitter() { ResizeDirection = GridResizeDirection.Rows, VerticalAlignment = VerticalAlignment.Stretch };
                    Grid.SetRow(splitter, i * 2 + 1);
                    MainGrid.Children.Add(splitter);
                }
            }
            MainGrid.Children.Add(Items[i]);
        }
    }

    private GridLength SizeToGridLength(double size)
    {
        return new GridLength(size, GridUnitType.Star);
    }

    public PanelControl()
    {
        InitializeComponent();
        Items.CollectionChanged += OnItemsCollectionChanged;
    }
}

我在这里使用它:

<p:PanelControl>
    <Button />
    <Button />
    <Button />
</p:PanelControl>

当我启动应用程序时,它运行良好,但在设计器中,我在xaml中加下了第一个按钮并且错误“指定的元素已经是另一个元素的逻辑子元素。首先断开它。”谢谢你的帮助,抱歉我的英语不好。

2 个答案:

答案 0 :(得分:2)

不确定设计师会发生什么,但这样可以修复&#39;它

更改行:

MainGrid.Children.Add(Items[i]);

要:

var parent = VisualTreeHelper.GetParent(Items[i]) as Grid;
if (parent != null)
    parent.Children.Remove(Items[i]);
MainGrid.Children.Add(Items[i]);

答案 1 :(得分:0)

使用JH的代码,我会把它移到你的函数的顶部,这样当你清除它时,你的网格的状态是&#34;清除&#34;,并且所有的孩子都已断开连接来自Visual Tree。

private void ClearAndBuildGrid()
{
    foreach (var item in Items)
    {
        var parent = System.Windows.Media.VisualTreeHelper.GetParent(item) as Grid;
        if (parent != null)
            parent.Children.Remove(item);
    }
    MainGrid.Children.Clear();

这将是一种风格/意图/偏好的事情,并且要清楚,答案是J.H.给出完全有效。

考虑使用foreach代替for而不必处理数组下标。

相关问题