有没有一种方法可以创建递归UWP用户控件?

时间:2020-04-16 17:26:18

标签: xaml recursion uwp

我有一个类,该类本身嵌入了一个ObservableCollection。

我正在尝试创建一个用户控件,该控件也具有对自身的引用,以便显示可观察集合的内容。但是,每当我尝试运行该应用程序时,都会遇到运行时错误。

该错误不是太有意义: XAML解析失败。 E_RUNTIME_SETVALUE [行:91位置:58](这是对用户控件进行递归调用的行)

该类看起来像这样(出于说明目的而被缩短了)

    public class BookChapterVm : IBookChapterVm
    {
    public int Id {get;set;}
    public string ChapterText {get;set;}
    public ObservableCollection<IBookChapterVm> Chapters { get; set; } = new ObservableCollection<IBookChapterVm>();
    }

用户控件看起来像这样(再次,删除了不必要的部分)

<UserControl
    x:Class="Cgs.Ux.UserControls.HelpTextEditor.BookChapterEditorCtrl">
            <ListView
                ItemsSource="{x:Bind Vm.Chapters, Mode=OneWay}">
                <ListView.ItemTemplate>
                    <DataTemplate x:DataType="help:BookChapterVm">
                        <StackPanel Orientation="Horizontal">
                            <local:BookChapterEditorCtrl Vm="{Binding}"/>
                        </StackPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
</UserControl>

我还尝试设置一个递归数据模板,但是它基本上以相同的错误结束。

1 个答案:

答案 0 :(得分:1)

这是一个工作示例:

在您的页面中:

<local:RecursiveContainer ViewModel="{Binding}" />

背后的代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = BuildBookChapterVM();
    }

    private BookChapterVM BuildBookChapterVM()
    {
        BookChapterVM vm1 = new BookChapterVM { ChapterText = "1" };
        BookChapterVM vm21 = new BookChapterVM { ChapterText = "21" };
        BookChapterVM vm22 = new BookChapterVM { ChapterText = "22" };
        BookChapterVM vm211 = new BookChapterVM { ChapterText = "211" };
        vm1.Chapters.Add(vm21);
        vm1.Chapters.Add(vm22);
        vm21.Chapters.Add(vm211);
        return vm1;
    }
}

public class BookChapterVM
{
    public int Id { get; set; }
    public string ChapterText { get; set; }
    public ObservableCollection<BookChapterVM> Chapters { get; set; } = new ObservableCollection<BookChapterVM>();
}

UserControl XAML:

<UserControl
    x:Class="WpfApp2.RecursiveContainer"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:WpfApp2"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    d:DesignHeight="450"
    d:DesignWidth="800"
    mc:Ignorable="d">
    <StackPanel>
        <TextBlock Text="{Binding ChapterText}" />
        <ItemsControl HorizontalContentAlignment="Stretch" ItemsSource="{Binding Chapters, Mode=OneWay}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <local:RecursiveContainer
                        Margin="10,5,0,5"
                        HorizontalAlignment="Stretch"
                        ViewModel="{Binding}" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </StackPanel>
</UserControl>

背后的UC代码:

public partial class RecursiveContainer : UserControl
{
    public RecursiveContainer()
    {
        InitializeComponent();
    }

    public BookChapterVM ViewModel
    {
        get { return (BookChapterVM)GetValue(ViewModelProperty); }
        set { SetValue(ViewModelProperty, value); }
    }

    public static readonly DependencyProperty ViewModelProperty =
        DependencyProperty.Register("ViewModel", typeof(RecursiveContainer), typeof(RecursiveContainer));
}

将图像视为概念证明。 希望对您有所帮助;)

enter image description here