Solved

problem in datatemplate in combobox

Posted on 2010-11-23
2
362 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
2 Comments
 
LVL 5

Accepted Solution

by:
buraksarica earned 500 total points
Comment Utility
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
Comment Utility
Thanks
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now