简体   繁体   中英

C# WPF Data grid display error: Object being displayed

I have assigned a list of reports to be displayed within a WPF datagrid.

All values which are properties of the report object are displaying fine, however the values for the objects contained within the reports constructor just displays a generic object name from the data grid and I'm unsure how to access any of the underlying properties from said objects to provide a more meaningful display to the user.

Report constructor:

 public Report(string reporterName, Asset asset, Location incidentLocation, Room room, string incidentType)
    {
        ReporterName = reporterName;
        Asset = asset;
        IncidentLocation = incidentLocation;
        Room = room;
        IncidentType = incidentType;
    }

Datagrid XAML:

<DataGrid Name="ReportsGrid" HorizontalAlignment="Left" Height="212" Margin="33,252,0,0" VerticalAlignment="Top" Width="725"/>

Setting the datagrid with a list of report object:

List<Report> reports = new List<Report>();
reports.Add(report);

ReportsGrid.ItemsSource = reports;

Current datagrid output

Any help or advice would be much appreciated. Thanks for your time.

When the columns are automatically generated, they will display the result of the ToString method for an object, because there is no way of knowing how the display an instance of a custom type.

In case you can manually create your columns, you can specify a property path for already existing column types like DataGridTextColumn . Suppose that Asset has a string property called Name .

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Reports}">
   <DataGrid.Columns>
      <DataGridTextColumn Header="Asset" Binding="{Binding Asset.Name}"/>
      <!-- ...other columns -->
   </DataGrid.Columns>
 </DataGrid>

If your data type is more complex, you can instead use a DataGridTemplateColumn and create custom data templates for displaying and editing.

<DataGridTemplateColumn Header="Asset">
   <DataGridTemplateColumn.CellTemplate>
      <DataTemplate DataType="{x:Type local:Report}">
         <TextBlock Text="{Binding Asset.Name}"/>
         <!-- ...other controls -->
      </DataTemplate>
   </DataGridTemplateColumn.CellTemplate>
   <DataGridTemplateColumn.CellEditingTemplate>
      <DataTemplate DataType="{x:Type local:Report}">
         <TextBox Text="{Binding Asset.Name}"/>
         <!-- ...other controls -->
      </DataTemplate>
   </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

If you absolutely need to auto-generate your columns, you need to hook into the AutoGeneratingColumn event in code-behind or in a custom behavior.

<DataGrid x:Name="MyDatagrid" ItemsSource="{Binding Reports}">
MyDatagrid.AutoGeneratingColumn += OnAutoGeneratingColumns;

Then change the automatically generated columns or even replace them. In the example below I replaced a column, but you could also just use the e.Column that is already generated, eg a DataGridTextColumn and set the binding in its Binding property in code just like in the above example for manually generated columns.

private void OnAutoGeneratingColumns(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
   if (e.Column.Header.Equals("Asset"))
   {
      // Create a new template column
      var templateColumn = new DataGridTemplateColumn
      {
         Header = e.PropertyName,
         // ...set other properties
      };

      // Create a data template (or get one from a resource dictionary)
      var template = new DataTemplate(typeof(Report));

      // ...populate the data template and set bindings if needed

      // Set the cell template
      templateColumn.CellTemplate = template;

      // Do the same as above for the editing template if needed
      templateColumn.CellEditingTemplate = template;

      // Overwrite the auto-generated column
      e.Column = templateColumn;
   }

   // ...handle other columns or leave them as they are
}

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