在TestEntryView.xaml.cs中
public partial class TestEntryView : UserControl
{
public ObservableCollection<TestFieldView> Fields {get;set;}
...
}
其中TestFieldView是一个UserControl。
<UserControl x:Class="STS2Editor.View.TestEntryView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:vw="clr-namespace:STS2Editor.View"
mc:Ignorable="d"
x:Name="testEntryView"
d:DesignHeight="300"
d:DesignWidth="427"
d:DataContext="{Binding TestEntry, Source={StaticResource Sample}}">
<Grid Background="{DynamicResource ButtonNormalBorder}" TextElement.Foreground="{DynamicResource TextBrush}">
<Border Background="{DynamicResource ControlBackgroundBrush}" BorderBrush="{DynamicResource ControlBackgroundBrush}" BorderThickness="4" CornerRadius="16">
<Grid Margin="4" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ScrollViewer Background="{DynamicResource ControlBackgroundBrush}" Grid.IsSharedSizeScope="True" Grid.Row="1">
<ItemsControl x:Name="fieldList" ItemsSource="{Binding Fields, ElementName=testEntryView}"/>
</ScrollViewer>
</Grid>
</Border>
</Grid>
绑定是正确的,但是当我窥视视觉树时,我的所有子项都由边框和内容呈现器组成,没有子视觉。
最佳答案
如果由于某种原因未在您的UserControl(TestFieldView)上调用LoadComponent并且Xaml无法正确解析,我只能重新创建您的情况。这在InitializeComponent中发生(例如,参见What does InitializeComponent() do, and how does it work in WPF?)。您确定在TestFieldView的构造函数中调用它吗?