Wayn0r
Wayn0r

Reputation: 27

WPF Data Binding vs. Inheritance - Bind to property of base

I've got the following setup.

Base class

public class ToolPathBaseItem
{
    private double _x, _y, _z;

    public double X
    {
        get { return _x; }
        set { _x = value; }
    }

    public double Y
    {
        get { return _y; }
        set { _y = value; }
    }
    public double Z
    {
        get { return _z; }
        set { _z = value; }
    }

    public ToolPathBaseItem(double x, double y, double z)
    {
        _x = x;
        _y = y;
        _z = z;
    }
}

And serveral classes like

public class CWToolPathItem : ToolPathBaseItem
{
    private double _nx, _ny, _nz;

    public CWToolPathItem(double x, double y, double z, double nx, double ny, double nz, CWCLRecord record)
        : base(x, y, z)
    {
        _nx = nx;
        _ny = ny;
        _nz = nz;
    }

    public double Nx
    {
        get { return _nx; }
        set { _nx = value; }
    }

    public double Ny
    {
        get { return _ny; }
        set { _ny = value; }
    }

    public double Nz
    {
        get { return _nz; }
        set { _nz = value; }
    }
}

There will be more classes which extend "ToolPathBaseItem" and which will have maybe the same PropertyNames like "Nx, Ny,...".

I've got a DataGrid which ItemsSource is binded to an

ObservableList<CWToolPathItem>

The first question is: How can I bind the

<DataGridTextColumn Header="X" Binding="{Binding HERETHEBINDING, StringFormat=N3}"/>

to the X-Property of the CWToolPathItem, because the X-Property is defined in the base class.

The second question is: If i've got several classes with same-named properties. How can i decide which class should be used?

I hope I have described my problem sufficiently. Thanks in advance for your help.

Upvotes: 0

Views: 1308

Answers (1)

Nitin Purohit
Nitin Purohit

Reputation: 18578

You can directly bind to Base class property

<DataGridTextColumn Header="X" Binding="{Binding X, StringFormat=N3}"/>

Upvotes: 1

Related Questions