(UWP) 如何从 DataTemplate 中访问我的代码隐藏文件?

问题描述 投票:0回答:1

这基本上是一个数据绑定问题。

简单地说,我有一个 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。

c# xaml uwp uwp-xaml
1个回答
0
投票

尝试使用

DataContext
绑定(而不是
TextBlock
)将
ListView
Page
ListView
设置为
ElementName
所在的
x:Bind

© www.soinside.com 2019 - 2024. All rights reserved.