Can't bind DataTable to WPF DataGrid
I am trying to bind a DataTable to DataGrid .
public class ViewModel
{
public DataTable Columns { get; set; }
public DataView ColumnsView { get; set; }
public ViewModel()
{
Columns = new DataTable();
Columns.Columns.Add(new DataColumn("Col1"));
DataRow dr = Columns.NewRow();
var data =new Header
{
Prop0 = "Prop0",
Prop1 = "Prop1",
Prop2 = "Prop2",
Prop3 = "Prop3",
Prop4 = false
};
dr[0] = data;
Columns.Rows.Add(dr);
ColumnsView = Columns.AsDataView();
}
}
public class Header
{
public string Prop0 { get; set; }
public string Prop1 { get; set; }
public string Prop2 { get; set; }
public string Prop3 { get; set; }
public bool Prop4 { get; set; }
}
Here is my XAML :
<DataGrid BorderThickness="0" ColumnWidth="*"
CanUserAddRows="False"
Background="Transparent"
AutoGenerateColumns="True"
ItemsSource="{Binding ColumnsView,
Mode=TwoWay,IsAsync=True,
UpdateSourceTrigger=PropertyChanged}"
HeadersVisibility="None">
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type
DataGridCell}">
<StackPanel>
<StackPanel.Resources>
<Style
TargetType="TextBlock">
<Setter
Property="TextAlignment"
Value="Center"></Setter>
</Style>
</StackPanel.Resources>
<TextBlock Text="{Binding
Prop0}" />
<TextBlock Text="{Binding
Prop1}" />
<TextBlock Text="{Binding
Prop2}" />
<TextBlock Text="{Binding
Prop3}" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.CellStyle>
<DataGrid.Columns>
<DataGridTemplateColumn >
<DataGridTemplateColumn.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate
TargetType="{x:Type
DataGridCell}">
<StackPanel>
<StackPanel.Resources>
<Style
TargetType="TextBlock">
<Setter
Property="TextAlignment"
Value="Right"></Setter>
<Setter
Property="Margin"
Value="0,0,3,0"></Setter>
</Style>
</StackPanel.Resources>
<TextBlock
Text="Vehicle Type"
/>
<TextBlock Text="Start
Date" />
<TextBlock Text="End
Date" />
<TextBlock Text="Day
of Op" />
<TextBlock
Text="Suspended" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGridTemplateColumn.CellStyle>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
I've created an instance of the Header class then putted that in a datarow
and added datarow to datatable . When I execute application I see that
textblocks are empty.
No comments:
Post a Comment