locked
Update ListView XAML Programmatically RRS feed

  • Question

  • User383636 posted

    I am trying to update a ListView's ViewCell using C# code. Basically I want to Iterate through the listview and change the visibility of certain things within the ViewCell. Here's some pseudo code to better represent what I am trying to do.

    ListView lv = listviewFromXaml; //get a populated listview from xaml
    
    for (int a =0; a < lv.ItemCount; a++){ //Iterate through list
        if (someBoolean){
        var child = lv.getChild(a); //Get ViewCell by position
            child.exampleLabel.IsVisible = false; //Change something about the view after some simple logic
        }
    }
    

    Is Something like this possible in a Cross Platform application? Or am I stuck using Bindings in the XAML? If it is possible, how would this be done?

    Friday, April 19, 2019 6:13 PM

Answers

  • User371688 posted

    What is the data structure that the Listview is populated with?

    Suppose the object of your data structure is as follows( NewItem):

     public class NewItem
        {
            public string NewsName { get; set; }
            public string ArticleText { get; set; }
    
            public bool isVisible { get; set; }   // Use this field to indicate whether the item is visible
        }
    

    Then add the NewItem to the binded ObservableCollection

      ObservableCollection<NewItem> NewsItemList = new ObservableCollection<NewItem>();
      NewItem initsta1 = new NewItem() { NewsName = "first", ArticleText = "testing....1...",isVisible= true };
      NewsItemList.Add(initsta1);
      listview.ItemsSource = NewsItemList;
    

    You just update the value of isVisible, then the ListVIew will update automatically from the binded collection.

    For example:

     <ListView x:Name="listview" HasUnevenRows="True">
                <ListView.ItemTemplate>
                    <DataTemplate >
                        <ViewCell>
                          <StackLayout Orientation="Vertical">
                            <Label Text = "{Binding NewsName}" FontSize="24" />
                            <Label Text = "{Binding ArticleText}"  IsVisible="{Binding isVisible }"/><!-- Please note  here  -->
                          </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, April 22, 2019 3:23 AM

All replies

  • User42522 posted

    @Almightytree said: I am trying to update a ListView's ViewCell using C# code. Basically I want to Iterate through the listview and change the visibility of certain things within the ViewCell. Here's some pseudo code to better represent what I am trying to do.

    ListView lv = listviewFromXaml; //get a populated listview from xaml
    
    for (int a =0; a < lv.ItemCount; a++){ //Iterate through list
      if (someBoolean){
      var child = lv.getChild(a); //Get ViewCell by position
          child.exampleLabel.IsVisible = false; //Change something about the view after some simple logic
      }
    }
    

    Is Something like this possible in a Cross Platform application? Or am I stuck using Bindings in the XAML? If it is possible, how would this be done?

    Yes, changing a Boolean variable in a ObservableCollection bound to the ListView is the best and easiest one. Then the ListView displayed changes automatically.

    However, your pseudo code also should work. However your way of getting the child element is not correct. You may have to do something like:

    var item = lv.Items[a];
    var child = item.Children[x].IsVisible = false;
    

    where x is the index of the element in the hierarchy.

    Friday, April 19, 2019 6:24 PM
  • User383636 posted

    @ShantimohanElchuri said: var item = lv.Items[a];

    Thanks for the quick response! Im having an issue with this line of code here. Is Items[a] meant to represent an array of objects? I can't get that line to compile because it states that "ListView does not contain a definition for Items". Am I missing a directive or is that just a placeholder?

    Friday, April 19, 2019 6:37 PM
  • User42522 posted

    @Almightytree said:

    @ShantimohanElchuri said: var item = lv.Items[a];

    Thanks for the quick response! Im having an issue with this line of code here. Is Items[a] meant to represent an array of objects? I can't get that line to compile because it states that "ListView does not contain a definition for Items". Am I missing a directive or is that just a placeholder?

    Sorry, I intermixed WPF with XF ListView. Yes there is no Items defined in XF. You can get to it through SelectedItem only. So best way is to set it through ObservableCollection Binding.

    Saturday, April 20, 2019 1:56 PM
  • User371688 posted

    What is the data structure that the Listview is populated with?

    Suppose the object of your data structure is as follows( NewItem):

     public class NewItem
        {
            public string NewsName { get; set; }
            public string ArticleText { get; set; }
    
            public bool isVisible { get; set; }   // Use this field to indicate whether the item is visible
        }
    

    Then add the NewItem to the binded ObservableCollection

      ObservableCollection<NewItem> NewsItemList = new ObservableCollection<NewItem>();
      NewItem initsta1 = new NewItem() { NewsName = "first", ArticleText = "testing....1...",isVisible= true };
      NewsItemList.Add(initsta1);
      listview.ItemsSource = NewsItemList;
    

    You just update the value of isVisible, then the ListVIew will update automatically from the binded collection.

    For example:

     <ListView x:Name="listview" HasUnevenRows="True">
                <ListView.ItemTemplate>
                    <DataTemplate >
                        <ViewCell>
                          <StackLayout Orientation="Vertical">
                            <Label Text = "{Binding NewsName}" FontSize="24" />
                            <Label Text = "{Binding ArticleText}"  IsVisible="{Binding isVisible }"/><!-- Please note  here  -->
                          </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, April 22, 2019 3:23 AM