简体   繁体   中英

How to show a list of objects with one extra property (which is not part of the class)?

I am developing a C# WPF application with Entity Framework 6, database first. Many classes are automatically created based on the structure of the SQL Server database.
I have ie a class Artist and a class Title. If I just want to show in WPF a list of Artists or Titles I bind the list to an observable collection of Artists or Titles and all is fine.
But sometime I want to show ie a list of Artists plus an extra field/property “Select This”. Ie the user will see a list of 10 artists with all the properties from the Artist class (generated by EF) and additionally I want to show a checkbox so that the user can select each Artist row.
I guess I can do this by creating a new class based on the Artist class and another new class based on the Title class, etc. But somehow I think there must be an easier way to do this. But how? Any suggestions?

That's what we have MVVM for..

You should have a view model in between your EF Generated models and view, wherein you can do all such manipulations, it might sound lengthy right now but I can assure you its the finest way going fwd.

And just to answer your query here you can have an additional checkbox column. for that

  • you need to use a DataGrid on UI,
  • have a checkbox column in grid,
  • bind each checkbox checked to a command/eventhandler..
  • get the item there and play with it.

You may be asking for how to populate a dataGrid using an anonymous type that is generated from a linq query, like the result of a join query and so on, here a basic example how that works :

first lets define a dataGrid and bind its column to the properties you are expecting in your anonymous type object

 <DataGrid x:Name="Dg" AutoGenerateColumns="False">
       <DataGrid.Columns>
           <DataGridTextColumn Header="Clm1" Binding="{Binding Clm1}"/>
           <DataGridTextColumn Header="Clm2" Binding="{Binding Clm2}"/>
           <DataGridCheckBoxColumn Header="Clm3" Binding="{Binding Clm3,Mode=OneWay}"/>
       </DataGrid.Columns>
   </DataGrid>

then here how to populate that grid using an anonymous type

 public partial class MainWindow : Window
{
    public ObservableCollection<Item> DataGridItems { get; set; }   
    public MainWindow()
    {
        InitializeComponent();
        DataGridItems=new ObservableCollection<Item>()
        {
            new Item()
            {
                Clm1 = "Item1"
            }, new Item()
            {
                Clm1 = "Item2"
            }, new Item()
            {
                Clm1 = "Item3"
            }
        };
        Dg.ItemsSource = DataGridItems.Select((item) =>

            new
            {
                Clm1 = item.Clm1,
                Clm2= "Item2",
                Clm3=true
            }
        );
    }
}

public class Item
{
    public String Clm1 { get; set; }      
}

make sure to use the same Binding property name used in the dataGrid.

The classes which EF creates are all PARTIAL classes, which means you can add more attributes to them and use them in your program without affecting the DB.

You can easily extend a class like this:

namespace Solution.Project.Model
{
  partial class Title
  {
    public bool IsChecked {get;set;}
  }
}

EDIT: Remember the namespace!

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