0

This is my XAML Code

    <DataGridTemplateColumn.CellEditingTemplate>
       <DataTemplate>
            <ComboBox Height="22" Name="MyCombobox"  
                      ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=Page},Path=Data._cmbxData}" 
                      DisplayMemberPath="Column1" SelectedValuePath="Column2"></ComboBox>
       </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>

and this is my CS code

        DataTable _cmbxData = new DataTable();
        _cmbxData.Columns.Add("Column1", typeof(int));
        _cmbxData.Columns.Add("Column2", typeof(String));

        _cmbxData.Rows.Add(new object[] { 1,"Value1"});
        _cmbxData.Rows.Add(new object[] { 2, "Value2" });
        _cmbxData.Rows.Add(new object[] { 3, "Value3" });

How can I bind this Datatable to Combobox in my Xaml. ??

Thank you All

Rohit
  • 10,056
  • 7
  • 50
  • 82

2 Answers2

0

This should work:

Assuming that:

dt - DataTable

myWindow - XAMLs window name

<Window
Name="myWindow"
...

Col_Name - Desired column name

<ComboBox Name="MyComboBox" HorizontalAlignment="Left" Margin="181,55,0,0" Width="120" ItemsSource="{Binding Path=dt, ElementName=myWindow}">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Label Content="{Binding Path=Col_Name}"/>
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>    
    </ComboBox>

Or use RelativeSource.

Tafari
  • 2,639
  • 4
  • 20
  • 28
0

Your variable _cmbxData should be declared as a property of your windows like this :

public DataTable CmbxData { get; set; }

Then your DataGridTemplateColumn should referenced this property, you can use RelativeSource or by naming you windows you can access its properties.

<Window x:Class="SO1.UI.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    x:Name="mainPage">
<Grid>
    <DataGrid x:Name="dataGridTest">
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="Test">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="Test" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <ComboBox Height="22" Name="MyCombobox"  
                          ItemsSource="{Binding ElementName=mainPage , Path=CmbxData}" 
                          DisplayMemberPath="Column1" SelectedValuePath="Column2">            </ComboBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

Of course you need to bind you datagrid with a source...juste for example this is my code-behind to test :

   public partial class MainWindow : Window
{
    public DataTable CmbxData { get; set; }
    public IList<string> Test { get; set; }
    public MainWindow()
    {
        InitializeComponent();

        CmbxData = new DataTable();
        CmbxData.Columns.Add("Column1", typeof(int));
        CmbxData.Columns.Add("Column2", typeof(String));

        CmbxData.Rows.Add(new object[] { 1, "Value1" });
        CmbxData.Rows.Add(new object[] { 2, "Value2" });
        CmbxData.Rows.Add(new object[] { 3, "Value3" });


        this.Test = new List<string>();
        this.Test.Add("Test 1");
        this.dataGridTest.ItemsSource = this.Test;

    }
}
FloChanz
  • 3,279
  • 1
  • 15
  • 15