我能夠重新創建XAML DataGridTextColumn:如何以編程方式創建WPF Toolkit DataGridTemplateColumn?
<tk:DataGridTextColumn
Binding="{Binding FirstName}"
Header="First Name"/>
在代碼這樣的:
DataGridTextColumn dgtc = new DataGridTextColumn();
dgtc.Header = propertyLabel;
dgtc.Binding = new Binding(propertyName);
theDataGrid.Columns.Add(dgtc);
但我怎麼重新以下DataGridTemplateColumn的代碼?
<tk:DataGridTemplateColumn Width="100">
<tk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Style="{DynamicResource ManageLinkStyle}"
Tag="{Binding Id}" Text="Delete" MouseDown="System_Delete_Click"/>
<TextBlock Text=" "/>
<TextBlock Style="{DynamicResource ManageLinkStyle}"
Tag="{Binding Id}" Text="Edit" MouseDown="System_Edit_Click"/>
</StackPanel>
</DataTemplate>
</tk:DataGridTemplateColumn.CellTemplate>
</tk:DataGridTemplateColumn>
即我被陷在限定CellTemplate:
DataGridTemplateColumn dgTemplateColumn = new DataGridTemplateColumn();
dgTemplateColumn.CellTemplate = new CellTemplate ...???
答:
謝謝阿蘭,只是指在XAML模板鍵很適合我的需要,這裏是我如何改變上述爲我工作:
XAML:
<Window.Resources>
<DataTemplate x:Key="manageAreaCellTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Style="{DynamicResource ManageLinkStyle}"
Tag="{Binding Id}" Text="Delete" MouseDown="System_Delete_Click"/>
<TextBlock Text=" "/>
<TextBlock Style="{DynamicResource ManageLinkStyle}"
Tag="{Binding Id}" Text="Edit" MouseDown="System_Edit_Click"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
後臺代碼:
DataGridTemplateColumn dgTemplateColumn = new DataGridTemplateColumn();
dgTemplateColumn.Width = new DataGridLength(1, DataGridLengthUnitType.Star);
dgTemplateColumn.Header = "Manage Options";
dgTemplateColumn.CellTemplate = this.FindResource("manageAreaCellTemplate") as DataTemplate;
theDataGrid.Columns.Add(dgTemplateColumn);
好主意,工作出色,併產生更清潔的代碼,謝謝! – 2009-11-23 13:25:51
很高興它的工作,如果它是標記爲回答。 – 2009-11-23 22:24:03