0

对不起,如果我的问题已经得到解答,我很可能没有使用正确的搜索条件。假设我有两个 `ObservableCollections。第一个是问题:

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

问题类看起来像:

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");
            }
        }
    }

}

第二个是 的集合QuestionElements,几乎相同,但它有Nostring Title。两者都有的原因是,根据语言设置,我们将在DataGridfor TitleeachCorespondingQuestionNo中显示正确的语言。目前我的 DataGrid 看起来像:

<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>

显示Title来自ObservableCollection<QuestionElements>No ==的 DataGridTextColumn 应该如何CorespondingQuestionNo

4

1 回答 1

1

您不能基于No纯 XAML 中的属性将绑定映射到另一个集合。您将不得不在某处编写一些代码。

您应该做的是创建一个QuestionViewModel包含您要绑定到的所有属性的类,即您应该将QuestionQuestionElements类型合并到一个公共视图模型类型T中,然后绑定到ObservableCollection<T>. 这毕竟是视图模型的用途。

于 2020-05-25T14:37:03.577 回答