views:

314

answers:

1

Hi all,

I have a class that contains data from some model. This class has metadata along with the actual value.

class ServerValue {

    public int SomeId {get;}
    public int SomeOtherId {get;}
    public DateTime LastChanged {get;}

    public object Value {get;set;}

    // this lets me show the value, but how do i update it from the grid?
    public override string ToString(){
        return Value.ToString();
     }
}

Now I also have a class MyDataTable that derives from DataTable that has all kind of logic. It calls the server, gets a bunch of ServerValues and puts them into Rows and Columns.

Finally I have a WPF DataGrid that I bind to the MyDataTable and the data are displayed, because the DataGrid calls ToString on each ServerValue and gets back the value for display. Hurray so far.

Now, I want to have two way databinding, so input on the grid is written back to the ServerValue. So I want to bind the grid cells to the Value property of the ServerValue instead of the ServerValue itself.

Right now the ServerValue of the DataGrid cell is just replaced with a string. I could work around this and all but I'd to try the elegant route first.

So I have a datatable with a complex type in cells and i want two-way databinding to a specific property of that type.

Is this possible? I've been googling on this and i can't anything on this.

Thanks in advance,

John

+1  A: 

What you want is a way to convert back and forth from your object to their text reprenstations.

Define a Converter for your Binding

http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx

Johan Buret
Wow that was quick! Looks promising, looking into it now. Thanks!
gjvdkamp
Hi, i got the converter and all, but it's not used by the grid, it's still displaying the Type of class inserted. This is how i hook up the converter: <DataGrid Name="SomeGrid" AutoGenerateColumns="True" ItemsSource="{Binding Converter={StaticResource TesterConverter}}"/>Do you know the correct syntax? I've seen examples of converters but they're all with datatemplates etc. Thanks in advance, John
gjvdkamp
What I need now is the XAML sample with your Datagrid
Johan Buret