这基本上是一个数据绑定问题。
简单地说,我有一个 ListView,它的项目是生成的,并使用 DataTemplate 来呈现这些项目。然而,在 DataTemplate 中,DataContext 更改为正在使用的 x:DataType,因此我无法访问位于代码隐藏 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 确实有自己的代码隐藏文件,除了传统的 Binding 之外,它还允许我使用 x:Bind。
尝试使用
DataContext
绑定(而不是 TextBlock
)将 ListView
中 Page
的 ListView
设置为 ElementName
所在的 x:Bind
。