2

I have a DataTable with two Columns that i will bind to all the ComboBoxes of a DataGridComboBoxColumn where one of the Columns will be the Text of the Items, and other will be the Values of the Items.

I know that the property called 'DisplayMemberPath' is where i specify the Name of the Column to be the Text of the Items, and to the values it have to be specified in the 'SelectedValuePath' property.

But one more time it's a problem to Bind whatever to a DataGridComboBoxColumn because it can't access the DataContext of the DataGrid.

So how i set a DataTable to be the ItemsSource of a DataGridComboBoxColum?

Example where what i want works in Code Behind:

TestClass test = new TestClass();

dataGrid.Columns.Add(new DataGridComboBoxColumn()
{
    Header = "City",
    DisplayMemberPath = "Cities",
    SelectedValuePath = "ID",
    ItemsSource = test.Dt.DefaultView,
});

Here is my XAML Code:

<Window x:Class="WpfApp3.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApp3
    Title="MainWindow" Height="350" Width="600">

    <Grid Name="grid1">
        <DataGrid Name="dataGrid" AutoGenerateColumns="False" MinColumnWidth="100">
            <DataGrid.Columns>
                <DataGridComboBoxColumn Header="City" DisplayMemberPath="Cities" SelectedValuePath="ID" ItemsSource="{Binding local:TestClass.Dt}"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

Here is my Code Behind code:

public class TestClass
{
    public TestClass()
    {
        (...)
        //Here i am loading my DataTable
    }

    private static DataTable dt;

    public static DataTable Dt
    {
        get { return dt; }
    }
}
Miguel
  • 907
  • 4
  • 26
  • 50
  • Possible dup: http://stackoverflow.com/questions/1724180/problem-binding-datagridcomboboxcolumn-itemssource – dugas Mar 29 '11 at 20:27

1 Answers1

3

Either,
A. explicity set the DataContext of ComboBox to your DataTable, OR
B. if DataTable is some part of the object that you have bound to the DataGrid, use RelativeSource in ComboBox binding.

You will need to post your code, if you need specific help for these scenarios.

publicgk
  • 3,170
  • 1
  • 26
  • 45
  • If helps you i will post a code example of what i want to do. But what i want is to access in my XAML a DataTable who is in the Code-Behind. I only need a way or understand how to do it. I know how to work with Static Resources, but if isn't a Static Resource and this DataTable can't be, i don't know and i can't understand how to do it. – Miguel Mar 29 '11 at 21:08
  • code will help, otherwise we will keep on making assumptions. – publicgk Mar 29 '11 at 21:38
  • I have posted the code to be more precise in what i need. The example in Code Behind works and do what i need, but i want to do it in XAML. – Miguel Mar 29 '11 at 23:56
  • then this seems to be related: [Problem binding to static property](http://stackoverflow.com/questions/936304/problem-binding-to-static-property) – publicgk Mar 30 '11 at 06:09