Click here to Skip to main content
11,639,765 members (59,493 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# WPF C#4.0
I am trying to create a ListBox view for show data, and I want it to contain a ListBox with a datatemplate for 2 columns "Product ID & Product Barcode"
I want to create it either using pure C# code or if possible load it through xaml? If I can create a template I can get in c# as a resource of sorts.
What I have made until now is: 

        <RowDefinition Height="37*" />
        <RowDefinition Height="88*" />

In Code Behind:

string typedString = txtCAuto.Text.ToUpper();
        List<string> autoList = new List<string>();
         prodDetails ps = SelProd4Sale();
        foreach (string item in ps.ProdBrcdList)
            if (!string.IsNullOrEmpty(txtCAuto.Text))
                if (item.StartsWith(typedString))
                    FrameworkElementFactory colProdID = new FrameworkElementFactory(typeof(TextBlock));
                    Binding prodID = new Binding(ps.ProdIDList.ToString());
                    colProdID.SetBinding(TextBlock.TextProperty, prodID);
                    FrameworkElementFactory colProdBarcode = new FrameworkElementFactory(typeof(TextBlock));
                    Binding prodBarcode = new Binding();
                    prodBarcode.Path = new PropertyPath(ps.ProdBrcdList.ToString());
                    colProdBarcode.SetBinding(TextBlock.TextProperty, prodBarcode);

                    FrameworkElementFactory sb = new FrameworkElementFactory(typeof(StackPanel));
                    dTemplate = new DataTemplate { VisualTree = sb };

        if (autoList.Count > 0)
            lbSuggestion.ItemTemplate = dTemplate;
            //lbSuggestion.ItemsSource = autoList;
            lbSuggestion.Visibility = Visibility.Visible;
        else if (txtCAuto.Text.Equals(""))
            lbSuggestion.Visibility = Visibility.Collapsed;
            lbSuggestion.ItemsSource = null;
            lbSuggestion.Visibility = Visibility.Collapsed;
            lbSuggestion.ItemsSource = null;
but there is no data appears, any suggestion please. thanks,
Posted 27-Dec-12 0:39am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Hi here quick go through how to do it:
//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");
//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");
//set up the notes textblock
FrameworkElementFactory notes = new FrameworkElementFactory(typeof(TextBlock));
notes.SetBinding(TextBlock.TextProperty, new Binding("Notes"));
notes.SetValue(TextBlock.ToolTipProperty, "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;
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Solved by Creating datatemplate in XAML

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Suvendu Shekhar Giri 330
1 CPallini 177
2 F-ES Sitecore 170
3 sasanka sekhar panda 155
4 142
0 OriginalGriff 9,081
1 Sergey Alexandrovich Kryukov 8,847
2 Mika Wendelius 7,027
3 Suvendu Shekhar Giri 2,650
4 F-ES Sitecore 2,558

Advertise | Privacy | Mobile
Web03 | 2.8.150731.1 | Last Updated 29 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100