Ahmed Ahmedov
Ahmed Ahmedov

Reputation: 592

WPF XAML bind a DataGridTextColumn to a different than the source ObservableCollection item property

Excuse me if my question is answered already, more likely I don't use the correct search criteria. Let say that I have two `ObservableCollections. The first one is the Questions:

private ObservableCollection<Question> _questions = new ObservableCollection<Question>();
    public ObservableCollection<Question> Questions
    {
        get { return _questions; }
        set
        {
            if (value != _questions)
            {
                _questions = value;
                OnPropertyChanged("Questions");
            }
        }
    }

and the Question class looks like:

public class Question : INotifyPropertyChanged
{

    public Question()
    {

    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    private int _no = -1;
    public int No
    {
        get
        {
            return _no;
        }
        set
        {
            if (_no != value)
            {
                _no = value;
                OnPropertyChanged("No");
            }
        }
    }

    private int _corespondingQuestionNo = -1;
    public int CorespondingQuestionNo
    {
        get
        {
            return _corespondingQuestionNo;
        }
        set
        {
            if (_corespondingQuestionNo != value)
            {
                _corespondingQuestionNo = value;
                OnPropertyChanged("CorespondingQuestionNo");
            }
        }
    }

}

The second one is a collection of QuestionElements, which is pretty much the same, but it has No and a string Title. The reason for having both is that depending on the language setting, we will show in the DataGrid the Titlefor each CorespondingQuestionNoat the proper language. Currently my DataGrid looks like:

<DataGrid x:Name="questionsDataGrid"
              ItemsSource="{Binding ElementName=casesDataGrid, Path=SelectedItem.Questions, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
              >
        <DataGrid.Columns>
            <DataGridTextColumn 
            Binding="{Binding Path=No, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
            >
            </DataGridTextColumn>

        </DataGrid.Columns>
    </DataGrid>

How should a DataGridTextColumn which shows the Title from ObservableCollection<QuestionElements> with No == CorespondingQuestionNo look like?

Upvotes: 0

Views: 53

Answers (1)

mm8
mm8

Reputation: 169420

You cannot map the binding to another collection based on the No property in pure XAML. You will have to write some code somewhere.

What you should do is to create a QuestionViewModel class that contains all properties that you want to bind to, i.e. you should merge the Question and QuestionElements types together into one common view model type T and then bind to an ObservableCollection<T>. This is that view models are used for after all.

Upvotes: 1

Related Questions