C# 递归UserControl子绑定
我正在尝试构建一个递归工作的UserControl。这样我的UserControl就包含了它自己,等等 XamlC# 递归UserControl子绑定,c#,wpf,xaml,data-binding,C#,Wpf,Xaml,Data Binding,我正在尝试构建一个递归工作的UserControl。这样我的UserControl就包含了它自己,等等 Xaml 代码隐藏 public partial class CustomInterfaceGrid : UserControl, INotifyPropertyChanged { IEnumerable<Object> m_Children; #region Init public CustomInterfaceGrid() {
代码隐藏
public partial class CustomInterfaceGrid : UserControl, INotifyPropertyChanged
{
IEnumerable<Object> m_Children;
#region Init
public CustomInterfaceGrid()
{
InitializeComponent();
}
#endregion Init
#region Properties
public static readonly DependencyProperty CustomItemsSourceProperty =
DependencyProperty.Register("CustomItemsSource", typeof(IEnumerable<Object>), typeof(CustomInterfaceGrid),new PropertyMetadata(ItemsSourceChanged));
private static void ItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
CustomInterfaceGrid cig = (CustomInterfaceGrid)d;
if (CustomItemsSourceProperty != null)
{
cig.m_Children = cig.getChildren();
}
}
public IEnumerable<Object> Children
{
get
{
return m_Children;
}
set
{
if (m_Children != value)
{
m_Children = value;
OnPropertyChanged();
}
}
}
public IEnumerable<Object> CustomItemsSource
{
get
{
return GetValue(CustomItemsSourceProperty) as IEnumerable<Object>;
}
set {
SetValue(CustomItemsSourceProperty, value);
OnPropertyChanged();
}
}
#endregion Properties
#region Methods
private ObservableCollection<Object> getChildren()
{
{
if (CustomItemsSource != null)
{
ObservableCollection<Object> list = new ObservableCollection<object>();
foreach (var item in CustomItemsSource)
{
list.Add(item);
}
return list;
}
else return null;
}
}
#endregion Methods
}
公共部分类CustomInterfaceGrid:UserControl,INotifyPropertyChanged
{
i数不清的m_儿童;
#区域初始化
公共CustomInterfaceGrid()
{
初始化组件();
}
#端域初始化
#区域属性
公共静态只读从属属性CustomItemsSourceProperty=
DependencyProperty.Register(“CustomItemsSource”、typeof(IEnumerable)、typeof(CustomInterfaceGrid)、new PropertyMetadata(ItemsSourceChanged));
私有静态无效项SourceChanged(DependencyObject d、DependencyPropertyChangedEventArgs e)
{
CustomInterfaceGrid cig=(CustomInterfaceGrid)d;
if(CustomItemsSourceProperty!=null)
{
cig.m_Children=cig.getChildren();
}
}
可数儿童的公共教育
{
得到
{
返回m_儿童;
}
设置
{
if(m_Children!=值)
{
m_子女=价值;
OnPropertyChanged();
}
}
}
公共IEnumerable CustomItemsSource
{
得到
{
将GetValue(CustomItemsSourceProperty)返回为IEnumerable;
}
设置{
设置值(CustomItemsSourceProperty,值);
OnPropertyChanged();
}
}
#端域属性
#区域方法
私有observeCollection getChildren()
{
{
如果(CustomItemsSource!=null)
{
ObservableCollection列表=新的ObservableCollection();
foreach(CustomItemsSource中的var项)
{
列表。添加(项目);
}
退货清单;
}
否则返回null;
}
}
#端域法
}
我的问题是,“孩子”不起作用(不过这是我的猜测)。如果我调试该应用程序,我会在子级中获取元素,因此它不是空的,但他不会显示任何内容,作为来自第一个CustomItemSource的空表,它获取一个可观察的集合。在您的示例中没有名为“SourceElement”的元素
如果要绑定到CustomInterfaceGrid
元素本身的属性,可以使用RelativeSource
属性:
<local:CustomInterfaceGrid
CustomItemsSource="{Binding Children, RelativeSource={RelativeSource Self}}" />
您是否考虑过层次结构数据模板?请看。是的,Rekshino,但我希望它适用于每种类型,而且据我所知,您需要了解HierarchycalDataTemplates的数据结构
<local:CustomInterfaceGrid
CustomItemsSource="{Binding Children, RelativeSource={RelativeSource Self}}" />