2013-05-22 21 views
0

我試圖創建一個區域控制,做下面的技巧如何在WPF設計與定製化解決錯誤

<wgc:RegionContentControl Region="HotDog"> 
    <Label>Foo</Label> 
    <Label>Bar</Label> 
</wgc:RegionContentControl> 

<wgc:RegionControl Region="HotDog"> 
</wgc:RegionControl> 

在運行時和設計時在RegionContentControl 定義的控件將獲得附加到RegionControl。例如,我可以使用它將注入狀態注入 狀態欄。

無論如何,它在運行時工作正常,有時在設計器中工作。 但是有時在設計師,我拿到

Element already has a logical parent. It must be detached from the 
old parent before it is attached to the new one. 

我的代碼實現上述模式的錯誤下面是。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Markup; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 

namespace My.Controls 
{ 
    public class RegionMessage { 
     public enum RegionAction { 
      Add, 
      Remove, 
     } 
     public string Region { get; set; } 
     public List<Control> Controls { get; set; } 
     public RegionAction Action { get; set; } 
    } 

    public class RegionControl : ItemsControl 
    { 
     public RegionControl() 
     { 
      ReactiveUI 
       .MessageBus.Current 
       .Listen<RegionMessage>() 
       .Subscribe(HandleRegionMessage); 
     } 



     public string Region 
     { 
      get { return (string)GetValue(RegionProperty); } 
      set { SetValue(RegionProperty, value); } 
     } 

     public static readonly DependencyProperty RegionProperty = 
      DependencyProperty.Register("Region", typeof(string), typeof(RegionControl), new PropertyMetadata("")); 


     private void HandleRegionMessage(RegionMessage obj) 
     { 
      if (obj.Region!=Region) 
      { 
       return; 
      } 
      if (obj.Action==RegionMessage.RegionAction.Add) 
      { 
       foreach (var item in obj.Controls) 
       { 
        this.Items.Add(item); 
       } 
      } 
      else 
      { 
       foreach (var item in obj.Controls) 
       { 
        this.Items.Remove(item); 
       } 

      } 
     } 
    } 

    [ContentProperty("Children")] 
    public class RegionContentControl : Control 
    { 
     static RegionContentControl() 
     { 
      DefaultStyleKeyProperty.OverrideMetadata(typeof(RegionContentControl), new FrameworkPropertyMetadata(typeof(RegionContentControl))); 
     } 

     public static readonly DependencyProperty ChildrenProperty = 
     DependencyProperty.Register("Children", typeof(List<Control>), typeof(RegionContentControl), new PropertyMetadata(new List<Control>())); 

     public List<Control> Children 
     { 
      get { return (List<Control>)GetValue(ChildrenProperty); } 
      set { SetValue(ChildrenProperty, value); } 
     } 

     public string Region 
     { 
      get { return (string)GetValue(RegionProperty); } 
      set { SetValue(RegionProperty, value); } 
     } 

     // Using a DependencyProperty as the backing store for Region. This enables animation, styling, binding, etc... 
     public static readonly DependencyProperty RegionProperty = 
      DependencyProperty.Register("Region", typeof(string), typeof(RegionContentControl), new PropertyMetadata("")); 



     public RegionContentControl() 
     { 
      this.LoadedObserver().Subscribe(Loaded); 
     } 

     private void Loaded(System.Reactive.EventPattern<RoutedEventArgs> obj) 
     { 
      ReactiveUI.MessageBus.Current.SendMessage(new RegionMessage() 
      { 
       Action = RegionMessage.RegionAction.Add 
       , 
       Controls = Children 
       , 
       Region = Region 
      }); 
     } 
    } 

} 

我確定我的問題涉及控件仍然認爲他們連接到RegionContentControl,但我不知道如何正確分離它們。有什麼建議麼?

回答

0

廣播之前,從兒童取出物品的伎倆

List<Control> _HiddenChildren; 

    private void Loaded(System.Reactive.EventPattern<RoutedEventArgs> obj) 
    { 
     if (_HiddenChildren==null) 
     { 
      _HiddenChildren = Children; 
      this.Children = new List<Control>(); 
     } 

     ReactiveUI.MessageBus.Current.SendMessage(new RegionMessage() 
      { Action = RegionMessage.RegionAction.Add 
      , Controls = _HiddenChildren 
      , Region = Region 
      }); 
    } 
相關問題