Solved

problem in datatemplate in combobox

Posted on 2010-11-23
2
374 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 500 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

627 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