那麼,你可以繼續前進,我猜。
這一小段代碼,幾乎你想要做什麼,我相信..
XAML:
<DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ContentControl Content="{Binding UI}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
代碼隱藏:
public partial class MainWindow : Window
{
public List<Model> Items { get; set; }
public MainWindow()
{
InitializeComponent();
var textblock = new TextBlock();
textblock.Text = "I'm a textblock";
var button = new Button();
button.Content = "I'm a button";
var combobox = new ComboBox();
combobox.Items.Add("Item1");
combobox.Items.Add("Item2");
this.Items = new List<Model>(new[] {
new Model(textblock),
new Model(button),
new Model(combobox)
});
this.DataContext = this;
}
public class Model
{
public UIElement UI { get; set; }
public Model(UIElement ui)
{
this.UI = ui;
}
}
}
完美的答案!太感謝了。 :-) –