简体   繁体   中英

Dynamic Listview content WPF

Hello Everyone I am looking for a way to create a ListView on my page that will change depending on what button is clicked.

For example if the View Users button is clicked I want the list view to show "First Name" Last Name" "Email", if the View Projects button is pushed I want the list view to show "Project Name" "Description"....

All this data is being pulled off a SQL server. I want the user to be basically be able to switch between different SQL statements I am creating via a button click and have the results shown in the ListView.

I am not sure if the ListView is the best option for this or how to approach this. I want to still maintain MVVM while designing this. I am just looking for some input or a point in the right direction.

Thanks!

Try creating a User class. Then when you call your SELECT * from dbo.Users SQL statement and are executing your reader you can simply create a new User object, parse all the sql data into the User object (for example a string for name, datetime for bday, etc...), and store said User object in a List that you created when you ran the sql statement. Then you can write a foreach(User u in ), create a string inside the loop and write the properties to said string, then call ListView.Items.Add(). Just format the string how you wish to achieve the results you desire

What I ended up doing was using a List view style data trigger to switch between two different grid views. In my ViewModel I have a boolean called ViewingProjects that gets set to true when my button is pressed. This switches me between two different item sources that I bind the grid view to, when a button is pressed I call the appropriate GetUser() GetProject() function which updates the observable collection I am bound to and then update the view.

 <ListView 
                            Margin="0,50,0,0"  
                            VerticalAlignment="Stretch" 
                            HorizontalAlignment="Stretch" 
                            HorizontalContentAlignment="Stretch">
                            <ListView.Style>
                                <Style TargetType="ListView">
                                    <Setter Property="MaxHeight" Value="150"/>
                                    <Setter Property="ItemsSource" Value="{Binding Users}"/>
                                        <Setter Property="View">
                                        <Setter.Value>
                                            <GridView AllowsColumnReorder="True" ColumnHeaderToolTip="Users">
                                                <GridViewColumn Header="First Name" Width="Auto" DisplayMemberBinding="{Binding FirstName}"/>
                                                <GridViewColumn Header="Last Name" Width="Auto" DisplayMemberBinding="{Binding LastName}"/>
                                                <GridViewColumn Header="User Name" Width="Auto" DisplayMemberBinding="{Binding UserName}"/>
                                                <GridViewColumn Header="Email" Width="Auto" DisplayMemberBinding="{Binding Email}"/>
                                                <GridViewColumn Header="Employee Type" Width="Auto" DisplayMemberBinding="{Binding EmployeeType}"/>
                                            </GridView>
                                        </Setter.Value>
                                    </Setter>
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding ViewingProjects}" Value="True">
                                            <Setter Property="ItemsSource" Value="{Binding ProjectsCount}"/>
                                                <Setter Property="View">
                                                <Setter.Value>
                                                    <GridView AllowsColumnReorder="True" ColumnHeaderToolTip="Projects">
                                                        <GridViewColumn Header="Project Name" Width="Auto" DisplayMemberBinding="{Binding Project_Name}"/>
                                                        <GridViewColumn Header="Phase" Width="Auto" DisplayMemberBinding="{Binding Phase}"/>
                                                        <GridViewColumn Header="Hours Logged" Width="Auto" DisplayMemberBinding="{Binding Total_Hours_Worked}"/>
                                                        <GridViewColumn Header="Date Started" Width="Auto" DisplayMemberBinding="{Binding StartDate}"/>
                                                        <GridViewColumn Header="Proposed End Date" Width="Auto" DisplayMemberBinding="{Binding ProposedCompletionDate}"/>
                                                    </GridView>
                                                </Setter.Value>
                                            </Setter>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </ListView.Style>
                        </ListView>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM