KaranGupta
asked on
problem in datatemplate in combobox
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
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;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER