Hudlommen Hudlommen - 1 month ago 9
C# Question

Updating List<> used in xaml and reload frame

So i have an XML file that i load data into a List<> from.
That means that i have a main list that contains all the data i need.

From that list i the create variable "sub" lists that i then will use to present data, as needed.

My main window (as for now) contains a frame that i use to load in pages.

My problem is, that i can't figure out how you change the data to be shown on a page, reload it and keep it from showing the standard data.

So i think there a two ways of attacking this, but i need advise on what is the right(working) way.

So on my page1 (loaded into the frame) i have the following code:

public sealed partial class Page1 : Page
{
public List<Book> Books;
public List<Book> ts1;

public Page1()
{
this.InitializeComponent();
Books = BookManager.GetBooks();
ts1 = Books.Where(p => p.bogNa == "Robert").ToList();
}

private void Update_Click(object sender, RoutedEventArgs e)
{
ts1 = Books.Where(p => p.bogNa != "Robert").ToList();

}
}


So i can with my button update the list, but obviously when i then reload the frame, we are just back to square one. Can i do it different from the page?

In my page1 xaml i have this code:

<GridView ItemsSource="{x:Bind ts1}" IsItemClickEnabled="True" SelectionMode="Multiple">
<GridView.ItemTemplate>
<DataTemplate x:DataType="data:Book">
<StackPanel>
<Grid Height="200">
<Grid.RowDefinitions>
<RowDefinition Height="3*" />
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>

<Image Grid.Row="0" Name="pic" Width="150" Source="{x:Bind bogCo }"/>
<TextBlock Grid.Row="1" FontSize ="20" Text="{x:Bind bogNa}" />
<TextBlock Grid.Row="2" FontSize ="16" Text="{x:Bind bogRe}"/>
<Button Grid.Row="3" Content="More Info" FontSize="16">
<Button.Flyout>
<Flyout x:Name="FlyoutTest">
<TextBlock Text="{x:Bind bogAr}">

</TextBlock>
</Flyout>
</Button.Flyout>
</Button>
</Grid>

</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>


Is there a way to change

<GridView ItemSource="{x:bind ts1}" ...


To a different page than page1, like the MainPage?

Iv'e looked into making a data template but i don't know is that is the way to go? because i don't want to spend my time on that, for now, if it leads to the same problems.

Answer

So i found a good solution: ObservableCollection

 public sealed partial class MainPage : Page
    {
        public List<dataRaw> t1;
        public List<dataRaw> t3;
        public ObservableCollection<dataRaw> t2;
        public MainPage()
        {
            this.InitializeComponent();
            //So you call the information from a class file (normally in Models)
            t1 = collectionGenerator.getList();
            //Then you use the t1 (List<T>) and convert it to an ObservableCollection
            t2 = new ObservableCollection<dataRaw>(t1);
        }

        private void Filter_Click(object sender, RoutedEventArgs e)
        {
            //Gather user input from a textbox (TB1)
            // clear the data in ObservableCollection t2
            t1 = collectionGenerator.getList();
            t2.Clear();
            string data = TB1.Text;

            //Check what input the user have entered
            if(data != "")
            {
            var Vtest = from VT in t1
                        where VT.firstName == data || VT.lastName == data
                        select VT;
                foreach (var VT in Vtest)
                {
                    //Add the given data back to the t2
                    t2.Add(new dataRaw { data = "1", firstName = VT.firstName, lastName = VT.lastName });

                }
            }
            else
            {
                var Vtest = from VT in t1
                            where VT.data == "1"
                            select VT;
                foreach (var VT in Vtest)
                {
                    t2.Add(new dataRaw { data = "1", firstName = VT.firstName, lastName = VT.lastName });

                }
            }
          }
        }