views:

93

answers:

1

Hello,

I am implementing a WPFToolkit datagrid and I want to bind it to it's source. Currently I do it programatically in code-behind through linking the query results to the ItemsSource . Still this is not effective enough, as I want to be able to select which columns to display and also to rename the headers, for which I need binding for each datagridtextcolumn sections.

I will appreciate any ideas. Thank you in advance!

+2  A: 

You can also set the bindings of each column in code-behind.

Here is a sample DataGrid with manually-defined columns with custom header text:

    <toolkit:DataGrid x:Name="dataGrid" AutoGenerateColumns="False">
        <toolkit:DataGrid.Columns>
            <toolkit:DataGridTextColumn x:Name="column1" Header="My Header" />
            <toolkit:DataGridTextColumn x:Name="column2" Header="Another Header" />
            <toolkit:DataGridTextColumn x:Name="column3" Header="Third Header" />
        </toolkit:DataGrid.Columns>
    </toolkit:DataGrid>

And here is some code-behind that binds each column to a property from the collection that is set to be the ItemsSource:

this.dataGrid.ItemsSource = this.testCollection; // replace with your collection
this.column1.Binding = new Binding("Property1"); // replace with your property names
this.column2.Binding = new Binding("Property2");
this.column3.Binding = new Binding("Property3");

You can change the header text and visibility in code if you like:

this.column1.Header = "New Header";
this.column2.Visibility = System.Windows.Visibility.Collapsed;

I'm just using a simple test class, Prop, with some string properties.

public class Prop
{
 public string Property1 { get; set; }
 public string Property2 { get; set; }
 public string Property3 { get; set; }

 public Prop(string p1, string p2, string p3)
 {
  this.Property1 = p1;
  this.Property2 = p2;
  this.Property3 = p3;
 }
}
Jeremy
Than you. I did not use exactly this, but this ed me to the idea. Timely assistance, dude :)
Branimir