我需要在ListView.GroupHeaderTemplate中的Switch中访问OnOff属性这个属性在ListView.ItemTemplate使用的集合中,我试过很多方面没有成功,有没有人遇到过这个问题?
>
<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>
答案 0 :(得分:2)
我认为您可以更改viewModel。每个groupHeaderTemplate都可以绑定一个值。我的代码如下:
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;
}
}
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" }
}
};
<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
会自动设置为空,因此您不需要设置ListView.GroupDisplayBinding
,但不要忘记IsGroupingEnabled="true"