如何使用自定义控件修复WPF设计中的错误

如何使用自定义控件修复WPF设计中的错误,wpf,custom-controls,reactiveui,Wpf,Custom Controls,Reactiveui,我正在尝试创建一个区域控件,它执行以下操作 <wgc:RegionContentControl Region="HotDog"> <Label>Foo</Label> <Label>Bar</Label> </wgc:RegionContentControl> <wgc:RegionControl Region="HotDog"> </wgc:RegionControl> 下面是实

我正在尝试创建一个区域控件,它执行以下操作

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

<wgc:RegionControl Region="HotDog">
</wgc:RegionControl>
下面是实现上述模式的代码

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

}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Markup;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
命名空间My.Controls
{
公共类区域消息{
公共枚举区域操作{
添加
去除
}
公共字符串区域{get;set;}
公共列表控件{get;set;}
公共区域操作操作{get;set;}
}
公共类RegionControl:ItemsControl
{
公共区域控制()
{
反应的
.MessageBus.Current
.听我说
.订阅(HandlerRegionMessage);
}
公共字符串区域
{
获取{return(string)GetValue(RegionProperty);}
set{SetValue(RegionProperty,value);}
}
公共静态只读DependencyProperty RegionProperty=
Register(“Region”、typeof(string)、typeof(RegionControl)、newpropertyMetadata(“”);
私有无效HandlerRegionMessage(RegionMessage obj)
{
if(对象区域!=区域)
{
返回;
}
if(obj.Action==RegionMessage.RegionAction.Add)
{
foreach(对象控件中的变量项)
{
此.Items.Add(item);
}
}
其他的
{
foreach(对象控件中的变量项)
{
此。项。删除(项);
}
}
}
}
[内容财产(“儿童”)]
公共类区域内容控制:控制
{
静态区域内容控制()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(RegionContentControl),new FrameworkPropertyMetadata(typeof(RegionContentControl));
}
公共静态只读从属属性ChildrenProperty=
Register(“Children”、typeof(List)、typeof(RegionContentControl)、newpropertyMetadata(new List());
公开儿童名单
{
获取{return(List)GetValue(ChildrenProperty);}
set{SetValue(ChildrenProperty,value);}
}
公共字符串区域
{
获取{return(string)GetValue(RegionProperty);}
set{SetValue(RegionProperty,value);}
}
//使用DependencyProperty作为区域的后备存储。这将启用动画、样式、绑定等。。。
公共静态只读DependencyProperty RegionProperty=
DependencyProperty.Register(“Region”、typeof(string)、typeof(RegionContentControl)、new PropertyMetadata(“”);
公共区域内容控制()
{
this.LoadedObserver().Subscribe(已加载);
}
已加载专用void(System.Reactive.EventPattern obj)
{
ReactiveUI.MessageBus.Current.SendMessage(新RegionMessage()
{
Action=RegionMessage.RegionAction.Add
,
控件=子对象
,
地区
});
}
}
}

我确信我的问题涉及到控件仍然认为它们连接到RegionContentControl,但我不确定如何正确地分离它们。有什么建议吗?

在播放前从孩子们身上移除这些物品就可以了

    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
            });
    }
List\u隐藏儿童;
已加载专用void(System.Reactive.EventPattern obj)
{
if(_HiddenChildren==null)
{
_隐藏儿童=儿童;
this.Children=新列表();
}
ReactiveUI.MessageBus.Current.SendMessage(新RegionMessage()
{Action=RegionMessage.RegionAction.Add
,控件=\u隐藏子对象
,Region=Region
});
}
    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
            });
    }