I'm using DataGrid from WPF Toolkit - The latest one. But the below code is not working
XAML code
<dg:DataGrid
Grid.Row="1"
Name="eventLogGrid"
Margin="5,0,5,0"
BorderBrush="Black"
ItemsSource="{Binding EventLogs}"
SelectionMode="Single"
IsReadOnly="True">
<dg:DataGrid.Columns>
<dg:DataGridTextColumn
Binding="{Binding EventID}" Header="Event ID" />
<dg:DataGridTextColumn
Binding="{Binding Server}" Header="Server" />
<dg:DataGridTextColumn
Binding="{Binding Source}" Header="Source" />
<dg:DataGridTextColumn
Binding="{Binding Logged}" Header="Logged" />
</dg:DataGrid.Columns>
</dg:DataGrid>
C# code
public class EventLogItem
{
public long EventID { get; set; }
public string Name { get; set; }
public string Message { get; set; }
public Server Server { get; set; }
public string Source { get; set; }
public EventLogEntryType Level { get; set; }
public DateTime Logged { get; set; }
public string Machine { get; set; }
}
public List<EventLogItem> EventLogs
{
get { return (List<EventLogItem>)GetValue(EventLogsProperty); }
set { SetValue(EventLogsProperty, value);
}
The real issue here is that although I specified the columns and its binding in the xaml code, but when the data arrived, every property is occupying a column. The desired effect is that only thosed bound properties are shown.
DataGrid has a property AutoGenerateColumns
, which is by default set to true, giving you the extra columns that you don't expect. Just set AutoGenerateColumns="False"
in your xaml declaration for DataGrid and you will get only the specified columns.
(Personally, I've always found this more of an annoyance than a convenience too!)