Help can't get TwoWay Binding to work with wpftoolkit:datagrid

Oct 5, 2008 at 1:43 AM

namespace

 

WpfApplication2

 

{

 

public class MassPair

 

{

 

private double massLow;
public double MassLow

 

{

 

get { return massLow; }

 

 

set { massLow = value; }

 

}

 

private double massHigh;

 

 

public double MassHigh

 

{

 

get { return massHigh; }

 

 

set { massHigh = value; }

 

}

}

 

public partial class Window1 : Window

 

{

 

DataGrid grid;

 

 

List<MassPair> foo;

 

 

public Window1()

 

{

InitializeComponent();

grid =

new DataGrid();

 

mygrid.Children.Add(grid);

 

Button btn2 = new Button();

 

btn2.Click +=

new RoutedEventHandler(btn_start_Click);

 

btn2.Content =

"Start";

 

mygrid.Children.Add(btn2);

 

Button btn1 = new Button();

 

btn1.Click +=

new RoutedEventHandler(btn1_Click);

 

btn1.Content =

"End";

 

mygrid.Children.Add(btn1);

foo =

new List<MassPair>();

 

foo.Add(

new MassPair { MassHigh = 100.0, MassLow = 10 });

 

foo.Add(

new MassPair { MassHigh = 101.0, MassLow = 20 });

 

foo.Add(

new MassPair { MassHigh = 102.0, MassLow = 30 });

 

}

 

void btn_start_Click(object sender, RoutedEventArgs e)

 

{

// This fails to do anything I'm not sure what path to give it so it binds both colums
grid.SetBinding(

DataGrid.ItemsSourceProperty, new Binding() { Source = foo, Path = new PropertyPath("MassLow"), Mode = BindingMode.TwoWay });

 

// This works fine
grid.SetBinding(

DataGrid.ItemsSourceProperty, new Binding() { Source = foo, Mode = BindingMode.OneWay });

 

}

 

void btn1_Click(object sender, RoutedEventArgs e)

 

{

// This copy works fine but I want to bind it so the model is updated as cells are edited
foo = (

List<MassPair>)grid.Items.SourceCollection;

 

}

}

}

Coordinator
Oct 6, 2008 at 3:18 AM
Try creating an ObservableColleciton<MassPair> instead of a List<MassPair>.  Also, setting grid.ItemsSource = foo should work fine without setting the binding explicitly.