2008-10-29 8 views
74

Estoy intentando crear una lista desplegable para una interoperabilidad winform, y estoy creando el menú desplegable en el código. Sin embargo, tengo problemas para vincular los datos según el DataTemplate que especifico.¿Cómo construyo un DataTemplate en código C#?

¿Qué me estoy perdiendo?

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; 

Respuesta

133

Asumiendo que ya ha creado para la ItemsSource etc ... para 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; 

Usted puede utilizar de la misma manera yo he dado la ToolTip en los TextBlock s establecer otras propiedades tales como márgenes.

+1

en Silverlight 4 clase frameworkelementfactory no está allí. No quiero usar xaml.load también ... ¿hay alguna otra manera con la que podamos resolverlo? – curiosity

+0

tampoco funciona en wp7 ... – swinefeaster

+1

Para silverlight 4/5 ... ref .: http://blogs.msdn.com/b/scmorris/archive/2008/04/14/defining-silverlight-datagrid-columns -at-runtime.aspx – Nordes

-2

Bueno, de hecho todavía tenemos otra manera, realmente te gustará si no te gustan los FrameworkElementFactory cosas.

Y creo que sólo hace que pequeños cambios en el código natural, es decir, declarar una UserControl y poner el mando en ella, y luego, utilizar un solo FrameworkElementFactory a llamar a la UserControl.

código de demostración simple (en Fa #):

let buildView()=StackPanel() 
//Build it with natural code 
type MyView()=inherit UserControl(Content=buildView()) 
let factory=FrameworkElementFactory(typeof<MyView>) 
let template=DataTemplate(VisualTree=factory) 
let list=ItemsControl(ItemsSource=makeData(),ItemTemplate=template) 
Cuestiones relacionadas