N
N
Nikita Permin2015-03-29 21:25:37
.NET
Nikita Permin, 2015-03-29 21:25:37

How to bind columns and rows in DataGrid in WPF?

How to bind 2D structures (like comparison tables) in a DataGrid with dynamically updating bindings? For example, there is an Alternative
class :

public class Alternative : ObservableObject
{
    private string _alternativeName = "";
    public string AlternativeName
    {
        get { return _alternativeName; }
        set
        {
            _alternativeName = value;
            RaisePropertyChanged();
        }
    }

    private ObservableCollection<Alternative> _childAlternatives = new ObservableCollection<Feature>();;
    public ObservableCollection<Alternative> ChildAlternatives
    {
        get { return _childAlternatives; }
        set
        {
            _childAlternatives = value;
            RaisePropertyChanged();
        }
    }

    private MappedValueCollection _childComparison = new MappedValueCollection();
    public MappedValueCollection ChildComparison
    {
        get{ return _childComparison; }
        set
        {
            _childComparison = value;
            RaisePropertyChanged();
        }
    }
}

And you need to get a table like this: OiVIUPl.png
Closest found solution: www.codeproject.com/Tips/676530/How-to-Add-Columns...
The use case assumes that the user will select Alternative from the TreeView , and the selected instance will be stored in the field SelectedAlternative , and its ChildComparison will automatically be drawn in the DataGrid. But the code from the codeproject link incorrectly switches between the MappedValueCollection: each time a new branch is selected, the entire content of its ChildComparison field is copied to the newly selected branch, and it looks like this:
Alternative tree - Bn1Byo3.png
And the content of RootNode.ChildComparison in the debugger -gTWF6Rs.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question