Рекурсивный пользовательский контроль UserControl - PullRequest
0 голосов
/ 26 февраля 2020

Я пытаюсь создать UserControl, который работает рекурсивно. Так что мой UserControl содержит себя и так далее.

Xaml

    <Grid>
        <DataGrid x:Name="DGrid" ItemsSource="{Binding CustomItemsSource, ElementName=SourceElement}" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="{Binding Children, ElementName=SourceElement}">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <local:CustomInterfaceGrid  Margin="0,10,0,0"  CustomItemsSource="{Binding Children, ElementName=SourceElement}"></local:CustomInterfaceGrid>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>

        </DataGrid>
    </Grid>
</UserControl>

CodeBehind

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

    }

Моя проблема в том, что «Дети» не работают (это мое предположение, хотя). Если я отлаживаю это приложение, я получаю Elements in Children, поэтому оно не пустое, но он ничего не отображает в виде пустых таблиц из первого CustomItemSource, который получает Observable Collection.

1 Ответ

0 голосов
/ 26 февраля 2020

В вашем примере нет элемента с именем «SourceElement».

Если вы хотите связать свойство самого элемента CustomInterfaceGrid, вы можете использовать свойство RelativeSource:

<local:CustomInterfaceGrid
    CustomItemsSource="{Binding Children, RelativeSource={RelativeSource Self}}" />
...