0

I'm completely new to WPF/XAML. I'm trying to work out XAML code to bind a DataTable to DataGrid. What I have is an instance of custom DataContainer class which implements INotifyPropertyChanged. This class has a property:

private DataTable totalsStatus = new DataTable();
public DataTable TotalsStatus
{
    get { return totalsStatus; }
    set
    {
        totalsStatus = value;
        NotifyPropertyChanged("TotalsStatus");
    }
}

now, in the C'tor of my MainWindow I have this, which works like a charm:

Binding b = new Binding();
b.Source = DataContainer;
b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
b.Path = new PropertyPath("TotalsStatus");
DataGridMain.SetBinding(DataGrid.ItemsSourceProperty, b);

How do I make this binding in XAML?

Yael
  • 1,566
  • 3
  • 18
  • 25
StaWho
  • 2,488
  • 17
  • 24
  • I am attempting to figure out how to resolve my issue from this question: I tried this in my code and it works the same as what I have for my DataGrid "_gridData.ItemsSource = ((DataTable)base.ItemsSource).DefaultView;" However how do you now programmatically in C# (no XAML) bind the DataGrid columns? – Let A Pro Do IT Apr 15 '14 at 00:53

1 Answers1

0

You need to use an objectdataprovider.

<ObjectDataProvider x:Key="yourdataproviderclass" 
                    ObjectType="{x:Type local:yourdataproviderclass}" />

<ObjectDataProvider x:Key="dtable" 
                    ObjectInstance="{StaticResource yourdataproviderclass}"
                    MethodName="GetTable"/> <!--here would be the method that returns your datasource-->

Then you can bind it to your datagrid in XAML with

<DataGrid ItemsSource="{Binding Source={StaticResource dtable}}" ></DataGrid>

There are different ways to do bindings in xaml though, so play around with it a bit.

Yael
  • 1,566
  • 3
  • 18
  • 25
Steinthor.palsson
  • 6,286
  • 13
  • 44
  • 51
  • At the end of day I decided to instantiate my class in XAML and FindResource() in code behind, it makes all the code much cleaner. – StaWho May 05 '11 at 09:11