Change the Glyph dinamically

Eduardo Gomez 3,426 Reputation points
2021-06-26T16:58:40.107+00:00

Hello I have a little issue, I want to change my Glyph, based on the order of the Listview

Here is my little method, to control that

    [AddINotifyPropertyChangedInterface]  
public class NotebooksVM {  
    public ObservableCollection<Notebook> NotebooksCollection { get; set; }  
    public ICommand ChangeLayoutCommand { get; set; }  
    public ICommand CreateNewNotebook { get; set; }  
    public ICommand SelectedNoteBookCommand { get; set; }  
    public Notebook SelectedNotebook { get; set; }  
    public LayoutBase LayoutBase { get; set; }  

    public NotebooksVM() {  

        LayoutBase = new LinearLayout();  

        NotebooksCollection = new ObservableCollection<Notebook>();  

        CreateNewNotebook = new Command(async () => {  
            var result = await Application.Current.MainPage.DisplayPromptAsync(string.Empty,  
                "Notebook name?");  

            if (!string.IsNullOrEmpty(result)) {  
                var notebook = new Notebook() {  
                    Name = result,  
                    UserId = App.UserId,  
                    CreatedAt = DateTime.Now.ToString("ddd, MMMM yyyy")  
                };  
                await Database.InsertAsync(notebook);  
                GetNotebooksAsync();  
            } else {  
                return;  
            }  
        });  
        GetNotebooksAsync();  
        SelectedNoteBookCommand = new Command(async () => {  
            await Application.Current.MainPage.Navigation.PushAsync(new NotesPage());  
            MessagingCenter.Send(this, "data", SelectedNotebook);  
            SelectedNotebook = null;  
  
        });  
        ChangeLayoutCommand = new Command(OnChangeLayout);  
          
    }  
    private async void GetNotebooksAsync() {  
        var notebooks = await Database.ReadAsync<Notebook>();  
        if (notebooks != null) {  
            notebooks.Where(notebook => notebook.Id == App.UserId);  
            NotebooksCollection.Clear();  
            foreach (var item in notebooks) {  
                NotebooksCollection.Add(item);  
            }  

        }  
    }  

    private void OnChangeLayout(object obj) {  
        if (LayoutBase is LinearLayout) {  
            LayoutBase = new GridLayout();  
        } else {  
            LayoutBase = new LinearLayout();  
        }  
    }  
}  

}
109556-glip.png

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,326 questions
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,951 Reputation points
    2021-06-28T06:35:30.343+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    To set binding for a property, it requires to specify the source. Please set BindingContext for the page, it should be the page class itemself in above code.

       public NotebooksVM()   
       {  
           ...  
           BindingContext = this;  
       }  
    

    It's recommend to use a model class to create the properties for data binding. Here is the related code, you could refer to.

       public class TestModel : INotifyPropertyChanged  
       {  
           bool testValue;  
         
           public TestModel(bool testValue)  
           {  
               TestCommand = new Command(testMethod);  
               this.testValue = testValue;  
           }  
         
           public ICommand TestCommand { get; set; }  
           private void testMethod(object obj)  
           {  
               if (testValue)  
               {  
                   IconSource = "one";  
               }  
               else  
               {  
                   IconSource = "grid_";  
               }  
           }  
         
           private string iconSource = "grid_";  
           public string IconSource  
           {  
               get  
               {  
                   return iconSource;  
               }  
               set  
               {  
                   if (iconSource != value)  
                   {  
                       iconSource = value;  
                       NotifyPropertyChanged();  
                   }  
               }  
           }  
         
           protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = "")  
           {  
               PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));  
           }  
         
           public event PropertyChangedEventHandler PropertyChanged;  
       }  
         
       public partial class TestPage : ContentPage  
       {  
           TestModel model;  
           public TestPage()  
           {  
               InitializeComponent();  
         
               model = new TestModel();  
               BindingContext = model;  
           }  
       }  
    

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful