相关疑难解决方法(0)

如何在c#代码中构建DataTemplate?

我正在尝试为winform互操作构建一个下拉列表,我正在创建代码中的下拉列表.但是,我在根据我指定的DataTemplate获取要绑定的数据时遇到问题.

我错过了什么?

drpCreditCardNumberWpf = new ComboBox();  
DataTemplate cardLayout = new DataTemplate {DataType = typeof (CreditCardPayment)};   
StackPanel sp = new StackPanel
{
    Orientation = System.Windows.Controls.Orientation.Vertical
};   

TextBlock cardHolder = new TextBlock {ToolTip = "Card Holder Name"};
cardHolder.SetBinding(TextBlock.TextProperty, "BillToName");
sp.Children.Add(cardHolder);

TextBlock cardNumber = new TextBlock {ToolTip = "Credit Card Number"};
cardNumber.SetBinding(TextBlock.TextProperty, "SafeNumber");
sp.Children.Add(cardNumber);

TextBlock notes = new TextBlock {ToolTip = "Notes"};
notes.SetBinding(TextBlock.TextProperty, "Notes");
sp.Children.Add(notes);

cardLayout.Resources.Add(sp, null);

drpCreditCardNumberWpf.ItemTemplate = cardLayout;
Run Code Online (Sandbox Code Playgroud)

c# wpf datatemplate

79
推荐指数
2
解决办法
8万
查看次数

在后面的代码中创建DataTemplate

如何以编程方式向datatemplates添加控件?

例如.下面我创建了TextBlock和DataTemplate.

TextBlock text = new TextBlock();
DataTemplate template = new DataTemplate();
Run Code Online (Sandbox Code Playgroud)

现在我需要将TextBlock添加到DataTemplate.怎么做到这一点?

我知道后面的代码中还有其他addind数据模板的方法1.在XAML中创建数据模板并将其加载到后面的代码上2.使用XamlParser创建和添加

但我需要按照我在示例中展示的方式来做.

需要一些帮助.

c# dynamic code-behind datatemplate

22
推荐指数
3
解决办法
3万
查看次数

标签 统计

c# ×2

datatemplate ×2

code-behind ×1

dynamic ×1

wpf ×1