Xamarin.Forms:如何访问ListView.GroupHeaderTemplate中ListView.ItemTemplate中使用的集合? [英] Xamarin.Forms: How to access the collection used in ListView.ItemTemplate within ListView.GroupHeaderTemplate?
问题描述
我需要访问ListView.GroupHeaderTemplate内的Switch中的OnOff属性,该属性在ListView.ItemTemplate使用的集合中,我已经尝试了许多方法而没有成功,有人遇到过这个问题吗?
I need to access the OnOff property in a Switch within the ListView.GroupHeaderTemplate this property is in the collection used by ListView.ItemTemplate, I have tried in many ways without success, has anyone ever had this problem?
>
<ListView
ItemsSource="{Binding ItemsGrouped}"
GroupShortNameBinding="{Binding Key}"
IsGroupingEnabled="true"
GroupDisplayBinding="{Binding Key}"
x:Name="ListViewAllDevices">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell.View>
<StackLayout>
<Label Text="{Binding Title}"/>
<Label Text="{Binding Description}"/>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.GroupHeaderTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout>
<Label Text="{Binding Key}" />
<Switch IsToggled="{Binding OnOff, Source={x:Reference Name=ListViewAllDevices}}"/>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.GroupHeaderTemplate>
推荐答案
我认为您可以更改viewModel.每个groupHeaderTemplate可以绑定一个值.我的代码如下:
I think you may to change the viewModel .Each groupHeaderTemplate can bind one value. My code as below:
public class Person
{
public string FullName
{
get;
set;
}
public string Address
{
get;
set;
}
}
public class Group : ObservableCollection<Person>
{
public Group(bool key)
{
Key = key;
}
public bool Key
{
get;
private set;
}
}
ItemSource
listView.ItemsSource = new[] {
new Group (true) {
new Person { FullName = "Ass" ,Address = "cole house" }
},
new Group (false) {
new Person { FullName = "Caprice Nave" }
},
new Group (false) {
new Person { FullName = "James Smith", Address = "404 Nowhere Street"},
new Person { FullName = "John Doe", Address = "404 Nowhere Ave" }
}
};
XMAL
<ListView x:Name="listView" IsGroupingEnabled="true">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout>
<Label Text="{Binding FullName}"/>
<Label Text="{Binding Address}"/>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.GroupHeaderTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout>
<Switch IsToggled="{Binding Key}"/>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.GroupHeaderTemplate>
</ListView>
PS:一旦setListView.GroupHeaderTemplate
,ListView.GroupDisplayBinding
将自动设置为null,因此您无需设置ListView.GroupDisplayBinding
,但不要忘记IsGroupingEnabled="true"
PS: Once you setListView.GroupHeaderTemplate
,ListView.GroupDisplayBinding
will set to null automatically, so you don't need to set ListView.GroupDisplayBinding
, but don't forget IsGroupingEnabled="true"
这篇关于Xamarin.Forms:如何访问ListView.GroupHeaderTemplate中ListView.ItemTemplate中使用的集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!