如何建立在c#code一个DataTemplate? [英] How do I build a DataTemplate in c# code?
本文介绍了如何建立在c#code一个DataTemplate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图建立一个winform互操作一个下拉列表,我创造了code中的下拉列表。不过,我已经得到的数据的问题,结合基础上的DataTemplate我指定。
我是什么失踪?
drpCreditCardNumberWpf =新的ComboBox();
DataTemplate中cardLayout =新的DataTemplate {数据类型= typeof运算(类:CreditCardPayment)};
StackPanel的SP =新的StackPanel
{
方向= System.Windows.Controls.Orientation.Vertical
};TextBlock的持卡人=新的TextBlock {工具提示=持卡人姓名};
cardHolder.SetBinding(TextBlock.TextPropertyBillToName);
sp.Children.Add(持卡人);TextBlock的cardNumber =新的TextBlock {工具提示=信用卡号码};
cardNumber.SetBinding(TextBlock.TextPropertySafeNumber);
sp.Children.Add(cardNumber);TextBlock的笔记=新的TextBlock {=工具提示注意事项};
notes.SetBinding(TextBlock.TextProperty,注意事项);
sp.Children.Add(注);cardLayout.Resources.Add(SP,NULL);drpCreditCardNumberWpf.ItemTemplate = cardLayout;
解决方案
假设你已经设置了的ItemsSource
等为 drpCreditCardNumberWpf
...
//创建数据模板
DataTemplate中cardLayout =新的DataTemplate();
cardLayout.DataType = typeof运算(类:CreditCardPayment);//设置堆栈面板
FrameworkElementFactory spFactory =新FrameworkElementFactory(typeof运算(StackPanel中));
spFactory.Name =myComboFactory;
spFactory.SetValue(StackPanel.OrientationProperty,Orientation.Horizontal);//设卡持有人文本块
FrameworkElementFactory持卡人=新FrameworkElementFactory(typeof运算(TextBlock中));
cardHolder.SetBinding(TextBlock.TextProperty,新的绑定(BillToName));
cardHolder.SetValue(TextBlock.ToolTipProperty,持卡人姓名);
spFactory.AppendChild(持卡人);//设置卡号文本块
FrameworkElementFactory cardNumber =新FrameworkElementFactory(typeof运算(TextBlock中));
cardNumber.SetBinding(TextBlock.TextProperty,新的绑定(SafeNumber));
cardNumber.SetValue(TextBlock.ToolTipProperty,信用卡号);
spFactory.AppendChild(cardNumber);//设置注释文本块
FrameworkElementFactory笔记=新FrameworkElementFactory(typeof运算(TextBlock中));
notes.SetBinding(TextBlock.TextProperty,新的绑定(说明));
notes.SetValue(TextBlock.ToolTipProperty,注意事项);
spFactory.AppendChild(注);//设置数据模板的可视化树
cardLayout.VisualTree = spFactory;//设置的项目模板是我们闪亮的新数据模板
drpCreditCardNumberWpf.ItemTemplate = cardLayout;
您可以使用,我设置了同样的方式工具提示
在的TextBlock
s到设置其他属性,如利润率。
I am trying to build a dropdown list for a winform interop, and I am creating the dropdown in code. However, I have a problem getting the data to bind based on the DataTemplate I specify.
What am I missing?
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;
解决方案
Assuming that you've already set up the ItemsSource
etc for drpCreditCardNumberWpf
...
//create the data template
DataTemplate cardLayout = new DataTemplate();
cardLayout.DataType = typeof(CreditCardPayment);
//set up the stack panel
FrameworkElementFactory spFactory = new FrameworkElementFactory(typeof(StackPanel));
spFactory.Name = "myComboFactory";
spFactory.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
//set up the card holder textblock
FrameworkElementFactory cardHolder = new FrameworkElementFactory(typeof(TextBlock));
cardHolder.SetBinding(TextBlock.TextProperty, new Binding("BillToName"));
cardHolder.SetValue(TextBlock.ToolTipProperty, "Card Holder Name");
spFactory.AppendChild(cardHolder);
//set up the card number textblock
FrameworkElementFactory cardNumber = new FrameworkElementFactory(typeof(TextBlock));
cardNumber.SetBinding(TextBlock.TextProperty, new Binding("SafeNumber"));
cardNumber.SetValue(TextBlock.ToolTipProperty, "Credit Card Number");
spFactory.AppendChild(cardNumber);
//set up the notes textblock
FrameworkElementFactory notes = new FrameworkElementFactory(typeof(TextBlock));
notes.SetBinding(TextBlock.TextProperty, new Binding("Notes"));
notes.SetValue(TextBlock.ToolTipProperty, "Notes");
spFactory.AppendChild(notes);
//set the visual tree of the data template
cardLayout.VisualTree = spFactory;
//set the item template to be our shiny new data template
drpCreditCardNumberWpf.ItemTemplate = cardLayout;
You can use the same way I have set the ToolTip
on the TextBlock
s to set other properties such as margins.
这篇关于如何建立在c#code一个DataTemplate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文