Мне нужен серьезный метод, который действительно может обновить мои свойства Xamarin ListView.
Все посты сообщают ObservableCollection<>
, и это работает только на Remove
.
.
По указанному коду:
Как изменить текст Button
сразу после включения Switch
?
(Внутри aa_Toggled()
)
DemoLists.XAML.CS
public partial class DemoLists : ContentPage/*, INotifyCollectionChanged*/
{
private ObservableCollection<Contact> _contacts;
public DemoLists()
{
InitializeComponent();
_contacts = GetContactList();
listView.ItemsSource = _contacts;
}
private ObservableCollection<Contact> GetContactList()
{
var l = new ObservableCollection<Contact>
{
new Contact{Name="Mosh", ImageUrl ="http://lorempixel.com/100/100/people/3", Status="t of child", MainTxtClr = PS.MainTextColor()},
new Contact{Name="Mos", ImageUrl ="http://lorempixel.com/100/100/people/3", Status="e of child", MainTxtClr = PS.MainTextColor()},
new Contact{Name="Mosad", ImageUrl ="http://lorempixel.com/100/100/people/3", Status="f of child", MainTxtClr = PS.MainTextColor()},
new Contact{Name="Mosasd", ImageUrl ="http://lorempixel.com/100/100/people/3", Status="g of child", MainTxtClr = PS.MainTextColor()},
new Contact{Name="Masd", ImageUrl ="http://lorempixel.com/100/100/people/3", Status="", MainTxtClr = PS.MainTextColor()},
new Contact{Name="Mash", ImageUrl ="http://lorempixel.com/100/100/people/3", Status="", MainTxtClr = PS.MainTextColor()},
new Contact{Name="Mesh", ImageUrl ="http://lorempixel.com/100/100/people/3", Status="", MainTxtClr = PS.MainTextColor()},
new Contact{Name="Mffsh", ImageUrl ="http://lorempixel.com/100/100/people/3", Status="", MainTxtClr = PS.MainTextColor()},
new Contact{Name="Moasfgh", ImageUrl ="http://lorempixel.com/100/100/people/3", Status="", MainTxtClr = PS.MainTextColor()},
new Contact{Name="John", ImageUrl="http://lorempixel.com/100/100/people/2", Status="Hey, let's talk!"},
new Contact{Name="Jasdfn", ImageUrl="http://lorempixel.com/100/100/people/2", Status="Hey, let's talk!"},
new Contact{Name="Joasfhn", ImageUrl="http://lorempixel.com/100/100/people/2", Status="Hey, let's talk!"},
new Contact{Name="Johhedfhren", ImageUrl="http://lorempixel.com/100/100/people/2", Status="Hey, let's talk!"},
new Contact{Name="Johewrgn", ImageUrl="http://lorempixel.com/100/100/people/2", Status="Hey, let's talk!"},
new Contact{Name="Johasdn", ImageUrl="http://lorempixel.com/100/100/people/2", Status="Hey, let's talk!"},
new Contact{Name="Johnzxv", ImageUrl="http://lorempixel.com/100/100/people/2", Status="Hey, let's talk!"}
};
return l;
}
private void aa_Toggled(object sender, ToggledEventArgs e)
{
var contact = (sender as Switch).BindingContext as Contact;
var esd = contact.IsON;
contact.ButtonText = esd.ToString();
}
}
DemoLists.XAML
<ListView x:Name="listView"
HasUnevenRows="True"
IsGroupingEnabled="False">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal" x:Name="cellLayout" Padding="0,0,20,0">
<Switch x:Name="aa" IsToggled="{Binding IsON}" Toggled="aa_Toggled" />
<StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand" >
<Label Text="{Binding Name}" TextColor="{Binding MainTxtClr}" FontSize="Small" FontAttributes="Bold"/>
<Label Text="{Binding Status}" />
</StackLayout>
<Button Text="{Binding ButtonText}"/>
</StackLayout>
<!--<ViewCell.ContextActions>
<MenuItem Text="Call" Clicked="Call_Clicked" CommandParameter="{Binding .}" />
<MenuItem Text="Delete" Clicked="Delete_Clicked" IsDestructive="True" CommandParameter="{Binding .}" />
</ViewCell.ContextActions>-->
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Contact.CS
class Contact : INotifyPropertyChanged
{
public bool IsON { get; set; }
public string Name { get; set; }
public string Status { get; set; }
public string ImageUrl { get; set; }
public Color MainTxtClr { get; set; }
public string ButtonText { get; set; }
public Contact()
{
MainTxtClr = PS.MainTextColor();
IsON = true;
ButtonText = "Follow Text";
}
}
Скриншоты