为WPF DataGridComboBox列绑定ItemsSource

时间:2022-08-08 17:02:46

Question: Most code samples on the DataGridComboBox seem to use a static resource as the ItemsSource. In my use case, I'd like to provide different ItemsSources with each bound object. Can this be done?

问题:DataGridComboBox上的大多数代码示例似乎都使用静态资源作为ItemsSource。在我的用例中,我想为每个绑定对象提供不同的itemssource。这个可以做吗?

Background: I'm trying to bind a collection of Question class objects to a WPF DataGrid, using a DataGridComboBoxColumn control. The Answer string provides the SelectedValue. I'd like the AnswerDomain list to provide the ItemsSource for each ComboBox. The AnswerDomain differs from Question to Question.

背景:我试图使用DataGridComboBoxColumn控件将问题类对象集合绑定到WPF数据网格。答案字符串提供SelectedValue。我希望AnswerDomain列表为每个ComboBox提供ItemsSource。答案域因问题而异。

Class

public class Question
  {
    string Answer {get; set;}
    List<string> AnswerDomain {get; set;}
    //...other stuff
  }

XAML

XAML

<DataGrid ItemsSource="{Binding Path=InspectionItems}" AutoGenerateColumns="False" Name="dataGrid1" >
    <DataGrid.Columns>
        <DataGridComboBoxColumn Header="Answer Domain"
                                DisplayMemberPath="Answer"
                                SelectedValuePath="Answer"
                                ItemsSource="{Binding Path=AnswerDomain}" 
                                    >
        </DataGridComboBoxColumn>
    </DataGrid.Columns>
</DataGrid>

Problem: There are a couple problems. The key issue right now is that the ComboBoxes in each DataGrid Row aren't displaying the AnswerDomain strings. I've tried a series of XAML combinations without success. Help me Stack Overflow.

问题:有几个问题。现在的关键问题是,每个DataGrid行中的combobox没有显示AnswerDomain字符串。我尝试了一系列没有成功的XAML组合。帮我堆栈溢出。

UPDATE: The selected solution below worked. After some further fumbling and by adding UpdateSourceTrigger=PropertyChanged to the SelectedItem, user changes in the combobox were then reflected back in the underlying custom object.

更新:下面所选的解决方案有效。在进一步摸索之后,通过向SelectedItem添加UpdateSourceTrigger=PropertyChanged, combobox中的用户更改随后会返回到底层的自定义对象中。

<DataGridTemplateColumn Header="Answer">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ComboBox 
                ItemsSource="{Binding AnswerDomain}" 
                SelectedItem="{Binding Answer, UpdateSourceTrigger=PropertyChanged}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

1 个解决方案

#1


10  

Your problem is that the display member path isn't Answer because there is no "Answer" property off of a string. I never use the DataGridComboBoxColumn, it doesn't seem natural to me, too much like the old win forms way. Try the below instead. BUT MAKE SURE YOU IMPLEMENT INotifyPropertyChanged on your Question Class, and fire the appropriate events.

您的问题是显示成员路径不是答案,因为字符串中没有“答案”属性。我从不使用DataGridComboBoxColumn,这对我来说不太自然,太像旧的win表单了。试一试下面。但是请确保在您的问题类中实现INotifyPropertyChanged,并触发适当的事件。

<DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ComboBox ItemsSource="{Binding AnswerDomain}" SelectedItem="{Binding Answer}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn> 

Here is how your Question class should look:

你的问题类应该是这样的:

public class Question : INotifyPropertyChanged
{
    private string m_Answer;
    public string Answer
    {
        get { return m_Answer; }
        set
        {
            if (m_Answer != value)
            {
                m_Answer = value;
                FirePropertyChanged("Answer");
            }
        }
    }

    private List<string> m_AnswerDomain;
    public List<string> AnswerDomain
    {
        get { return m_AnswerDomain; }
        set
        {
            if (m_AnswerDomain != value)
            {
                m_AnswerDomain = value;
                FirePropertyChanged("AnswerDomain");
            }
        }
    }


    [field: NonSerialized]
    public event PropertyChangedEventHandler PropertyChanged;

    private void FirePropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }


}

#1


10  

Your problem is that the display member path isn't Answer because there is no "Answer" property off of a string. I never use the DataGridComboBoxColumn, it doesn't seem natural to me, too much like the old win forms way. Try the below instead. BUT MAKE SURE YOU IMPLEMENT INotifyPropertyChanged on your Question Class, and fire the appropriate events.

您的问题是显示成员路径不是答案,因为字符串中没有“答案”属性。我从不使用DataGridComboBoxColumn,这对我来说不太自然,太像旧的win表单了。试一试下面。但是请确保在您的问题类中实现INotifyPropertyChanged,并触发适当的事件。

<DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ComboBox ItemsSource="{Binding AnswerDomain}" SelectedItem="{Binding Answer}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn> 

Here is how your Question class should look:

你的问题类应该是这样的:

public class Question : INotifyPropertyChanged
{
    private string m_Answer;
    public string Answer
    {
        get { return m_Answer; }
        set
        {
            if (m_Answer != value)
            {
                m_Answer = value;
                FirePropertyChanged("Answer");
            }
        }
    }

    private List<string> m_AnswerDomain;
    public List<string> AnswerDomain
    {
        get { return m_AnswerDomain; }
        set
        {
            if (m_AnswerDomain != value)
            {
                m_AnswerDomain = value;
                FirePropertyChanged("AnswerDomain");
            }
        }
    }


    [field: NonSerialized]
    public event PropertyChangedEventHandler PropertyChanged;

    private void FirePropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }


}