Solved

problem in datatemplate in combobox

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

910 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

22 Experts available now in Live!

Get 1:1 Help Now