我正在使用Visual Studio 2010中的C#上的wpf。通过sqlserver创建DataGrid并想设置Header的大小我的c#代码是
private void Window_Loaded(object sender, RoutedEventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\FUDA\\fuda.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
SqlDataAdapter da = new SqlDataAdapter();
con.Open();
DataTable dt = new DataTable();
da.SelectCommand = new SqlCommand("select * from user_ctrl",con);
da.Fill(dt);
dataGrid1.ItemsSource = dt.DefaultView;
}
Xaml Code是
<TabControl Height="311" HorizontalAlignment="Left" Name="tabControl1" VerticalAlignment="Top" Width="503">
<TabItem Header="tabItem1" Name="tabItem1">
<Grid>
<DataGrid Height="278" HorizontalAlignment="Left" Margin="1,1,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="492" />
</Grid>
</TabItem>
</TabControl>
上面的代码工作正常,但每当我尝试添加xaml自定义大小到Header时,给我错误“在'DataGrid'类型中找不到可附加属性'列'” 注意:我已禁用“自动生成列”
答案 0 :(得分:0)
当我将它放在xaml中时,它对我有用:
<TabControl Height="311" HorizontalAlignment="Left" Name="tabControl1" VerticalAlignment="Top" Width="503">
<TabItem Header="tabItem1" Name="tabItem1">
<Grid>
<DataGrid Height="278" HorizontalAlignment="Left" Margin="1,1,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="492">
<DataGrid.Columns>
<DataGridTextColumn Width="100" Header="Column 1" Binding="{Binding}" />
<DataGridTextColumn Width="100" Header="Column 2" Binding="{Binding}" />
<DataGridTextColumn Width="100" Header="Column 3" Binding="{Binding}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</TabItem>
</TabControl>
没有错误被我抛出......我希望这对你有所帮助:)