我想实现一个功能,如果我打开ListView中的任何开关,其他开关应该关闭。在ListView里面一次只打开一个开关
以下是我的XAML:
<ListView ItemsSource="{Binding AllProfiles}" HasUnevenRows="True" SeparatorVisibility="None" RowHeight="-1">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Vertical" BackgroundColor="#F6F6F6">
<Frame Padding="2" BackgroundColor="#FFFFFF" Margin="10,5,10,5">
<StackLayout Orientation="Horizontal" BackgroundColor="#FFFFFF" Padding="20">
<StackLayout Orientation="Vertical">
<Label Text="{Binding Name}" TextColor="#FF6D9D" FontSize="25"></Label>
<Label Text="{Binding Email}" TextColor="#B1B1B1" FontSize="18"></Label>
</StackLayout>
<Switch HorizontalOptions="EndAndExpand" VerticalOptions="CenterAndExpand" IsToggled="{Binding IsActive}">
</Switch>
</StackLayout>
</Frame>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我使用MVVM绑定,并试图改变事件的数据 'OnPropertyChanged( “IsActive”)',但无法实施。
任何建议都会有帮助。
编辑:
public class SetProfileViewModel:ViewModelBase
{
private ObservableCollection<Profile> _allProfiles = new ObservableCollection<Profile>();
public ObservableCollection<Profile> AllProfiles
{
get { return _allProfiles; }
set
{
_allProfiles = value;
OnPropertyChanged("AllProfiles");
}
}
}
public class Profile:ViewModelBase
{
private string _name;
private string _email;
private bool _isActive;
public string Name {
get { return _name; }
set
{
_name = value;
OnPropertyChanged("Name");
}
}
public string Email
{
get { return _email; }
set
{
_email = value;
OnPropertyChanged("Email");
}
}
public bool IsActive
{
get { return _isActive; }
set
{
_isActive = value;
OnPropertyChanged("IsActive");
//How can I change 'AllProfiles' data from here.
}
}
}
如果您正在使用数据绑定,则应该遍历“AllProfiles”集合中的项并将该属性设置为false。 –
@GeraldVersluis ..谢谢你的回应。我做了改变。请参阅编辑。我试图在Profile类下的'IsActive'中更改'AllProfiles'的项目数据。我怎样才能做到这一点? –