这基本上是一个数据绑定问题。
简单地说,我有一个ListView,它的项是生成的,并使用DataTemplate来呈现这些项。然而,在DataTemplate中,DataContext会更改为正在使用的x:DataType,因此我无法访问位于我的code-behind C#文件中的属性,甚至无法访问ViewModel中包含的属性。
希望我表达得清楚:
<ListView
x:Name="MyListView"
ItemsSource="{x:Bind mainViewModel.AdvancedNoteCollection, Mode=OneWay}">
<ListView.ItemTemplate>
<DataTemplate x:DataType="Models:Note">
<Grid>
<TextBlock Text="{ When trying to bind, I am stuck within the DataContext of the Note data type! }" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
然而,在现实中,我的DataTemplate位于外部ResourceDictionary文件中,这让我的情况有点更具挑战性。尽管外部ResourceDictionary有自己的代码后备文件,但我仍可以使用x:Bind除了传统的Binding。