I am new in c#. How can I bind something like "IsSelected" on xaml ListView to change selection specific items from code?
For example there is class Item with property IsGroupSelected that I want bind.
Also question: "How can I known that the myListViewCollection is finished render after add a lot of new items (so that the func myListViewCollection.ContainerFromItem(item) not return null)?"
MainPage.xaml
...
<ListView
x:Name="myListViewCollection"
VerticalAlignment="Top"
Margin="0,20,0,0"
SelectionMode="Multiple">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<TextBlock Text="{Binding Path = Title}" FontWeight="Bold"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
MainPage.xaml.cs
public class Item : INotifyPropertyChanged
{
private bool selectedGroup = false;
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public string Title { get; set; }
public bool IsGroupSelected
{
get
{
return this.selectedGroup;
}
set
{
this.selectedGroup = value;
NotifyPropertyChanged("IsGroupSelected");
}
}
//public ObservableCollection<SubItem> SubItems { get; set; }
public Item(string title, ObservableCollection<SubItem> subItems)
{
this.Title = title;
//this.SubItems = subItems;
}
};
public sealed partial class MainPage : Page
{
public ObservableCollection<Item> collectionItems = new ObservableCollection<Item>();
public MainPage()
{
this.InitializeComponent();
this.myListViewCollection.ItemsSource = this.collectionItems;
this.Loaded += MainPage_Loaded;
}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
collectionItems.Add(new Item("Title_1", null));
collectionItems.Add(new Item("Title_2", null));
collectionItems.Add(new Item("Title_3", null));
collectionItems[1].IsGroupSelected = true;
}
}