用户控制数据绑定winrt



我面临用户控件中的数据绑定问题。

<UserControl.Resources>
<CollectionViewSource
x:Name="itemsViewSource"
Source="{Binding Items}"
d:Source="{Binding AllGroups[0].Items, Source={d:DesignInstance Type=data:SampleDataSource, IsDesignTimeCreatable=True}}"/>
</UserControl.Resources>
<UserControl.DataContext>
<Binding>
<Binding.Source>
<local:SampleDataSource />
</Binding.Source>
</Binding>
</UserControl.DataContext>
<StackPanel Margin="20,60">
<TextBlock Style="{StaticResource PageHeaderTextStyle}">Categories</TextBlock>
<ListBox>
<ListBoxItem Content="{Binding Title}" />

</ListBox>
</StackPanel>

Title属性绑定显示在XAML设计器窗口中,但在程序执行期间没有显示任何内容。我使用的是默认的翻转视图布局,其中SampleDataSource.cs文件中有一个"标题">

"d:"前缀用于仅为在设计器中使用而定义的属性,因此应尝试将"d:Source"替换为"Source"。

您修复了以下行吗

DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"

它是在所有页面声明所在的顶部定义的。

还有

Source="{Binding Items}"

是指您的DataContext中名为ItemsList

最新更新