我无法让HeaderTemplate
中的{Binding Title}
出现。
这是与BindingContext
相关联的类:
class SensorGroup
{
public string Title { get; set; }
public IList<Sensor> Sensors { get; set; }
}
XAML:
<ListView Header=""
ItemsSource="{Binding Sensors}">
<ListView.HeaderTemplate>
<DataTemplate>
<Grid>
<Label Text="{Binding Title}"/>
</Grid>
</DataTemplate>
</ListView.HeaderTemplate>
<ListView.ItemTemplate>
...
</ListView>
如果我将其替换为
<Label Text="Some static text"/>
,文本会显示出来。我找到了与此相关的问题(链接在此处),它链接到另一个问题(链接在此处)。但是我无法使其工作。我尝试过:
<ContentPage.Resources>
<Label x:Key="MyTitle"
Binding="{Title}"/>
</ContentPage.Resources>
...
<Grid>
<StaticResource ResourceKey="MyTitle"/>
</Grid>
它给我一个错误,说找不到与Title
的绑定。
ListView
的项绑定到Sensors
属性,显示正确,因此构造函数中写入的BindingContext = new SensorGroup()
(过于简化)正在正常工作。 - Xavier Peña