Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

problem in datatemplate in combobox

Posted on 2010-11-23
2
Medium Priority
?
383 Views
Last Modified: 2012-08-13
Hi

I have created a combobox and along with that I have created a datatemplate. In the code behind I am binding the combobox with a datatable. But I am not getting the datatable.


Please check the code section.


What is going wrong.


Regards
Karan Gupta
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
    <Grid>
        <Grid.Resources>
            <DataTemplate x:Key="ComboBoxDataTemplate">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="0.2*"></ColumnDefinition>
                        <ColumnDefinition Width="0.8*"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <CheckBox Grid.Column="0" Width="10" Height="10"></CheckBox>
                    <TextBlock Grid.Column="1" Text="{Binding Text}"></TextBlock>
                </Grid>
            </DataTemplate>
        </Grid.Resources>
        <ComboBox x:Name="Test" ItemsSource="{Binding}" 
                  DisplayMemberPath="Drug" 
                  SelectedValuePath="ID"
                  Width="100" Height="20" ItemTemplate="{StaticResource ComboBoxDataTemplate}">            
        </ComboBox>
    </Grid>
</Window>


-----------------Code Behind---------------------------
public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        private string lcl_SelectedText;
        public string SelectedText
        {
            get { return lcl_SelectedText; }
            set { lcl_SelectedText = value; }
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            DataTable dt = GetTable();
            Test.DataContext = dt;
            Test.SelectedValue = 1;
        }
        static DataTable GetTable()
        {
            //
            // Here we create a DataTable with four columns.
            //
            DataTable table = new DataTable();
            table.Columns.Add("ID", typeof(int));
            table.Columns.Add("Drug", typeof(string));           

            //
            // Here we add five DataRows.
            //
            table.Rows.Add(1, "Indocin");
            table.Rows.Add(2, "Enebrel");
            table.Rows.Add(3, "Hydralazine");
            table.Rows.Add(4, "Combivent");
            table.Rows.Add(5, "Dilantin");
            return table;
        }

    }

Open in new window

0
Comment
Question by:KaranGupta
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 5

Accepted Solution

by:
buraksarica earned 2000 total points
ID: 34195284
You can not use Display Member and Itemtemplate at the same time.
Just remove the displaymember setting and change the DataTemplate like following:

<Window x:Class="Stadion.cbDt"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="cbDt" Height="300" Width="300">
    <Grid>
        <Grid.Resources>
            <DataTemplate x:Key="ComboBoxDataTemplate">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="0.2*"></ColumnDefinition>
                        <ColumnDefinition Width="0.8*"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <CheckBox Grid.Column="0" Width="10" Height="10"></CheckBox>
                    <TextBlock Grid.Column="1" Text="{Binding Drug}"></TextBlock>
                </Grid>
            </DataTemplate>
        </Grid.Resources>
        <ComboBox x:Name="Test" ItemsSource="{Binding}"                   
                  SelectedValuePath="ID"
                  Width="100" Height="20" ItemTemplate="{StaticResource ComboBoxDataTemplate}">
        </ComboBox>
    </Grid>
</Window>

Open in new window

0
 

Author Closing Comment

by:KaranGupta
ID: 34195379
Thanks
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question