Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

problem in datatemplate in combobox

Posted on 2010-11-23
2
Medium Priority
?
393 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 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

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

916 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