如何将列表绑定到Data Grids数据源的子类中的组合框?

时间:2011-02-21 05:49:35

标签: c# mvvm binding combobox wpfdatagrid

到目前为止,这是我的DxGrid代码:

<dxg:GridControl x:Name="gridControlItemsDistinct" 
                             VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
                             Height="Auto" Width="Auto"
                             AutoPopulateColumns="False"
                             DataSource="{Binding Path=ChaseListHelperClassItemsList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<dxg:GridControl.Columns>
    <dxg:GridColumn FieldName="CreativeContactID" Header="Creative Contact Name" Width="90" FilterPopupMode="CheckedList" AllowEditing="True">
        <dxg:GridColumn.EditSettings>
            <dxe:ComboBoxEditSettings Name="cboCreativeContact"
                            DisplayMember="ContactName" 
                            AutoComplete="True" 
                            ValueMember="ContactID" 
                            ItemsSource="{Binding Path=Data.CreativeAgContactList, UpdateSourceTrigger=PropertyChanged}" />
        </dxg:GridColumn.EditSettings>
        <dxg:GridColumn.CellStyle>
            <Style TargetType="{x:Type dxg:CellContentPresenter}">
                <Setter Property="Background" Value="#FFE8F2F3" />
                <Setter Property="TextBlock.Foreground" Value="Black" />
            </Style>
        </dxg:GridColumn.CellStyle>
    </dxg:GridColumn>
</dxg:GridControl.Columns>

如您所见,我的网格绑定到ChaseListHelperClassItemsList

public List<CreateChaseListHelperClass> ChaseListHelperClassItemsList
    {
        get 
        {
            return chaseListHelperClassItemsList; 
        }
        set
        {
            chaseListHelperClassItemsList = value;
            OnPropertyChanged("ChaseListHelperClassItemsList");
        }
    }

CreateChaseListHelperClass:

    public class CreateChaseListHelperClass
    {
        private int creativeContactID;

        public int CreativeContactID
        {
            get;
            set;
        }

        public IQueryable<Contacts> CreativeAgContactList
        {
            get;
        }
    }

我的问题是,如何将cboCreativeContact ItemSource绑定到CreateChaseListHelperClassCreativeAgContactList内的列表。

我目前的绑定如下:

ItemsSource="{Binding Path=Data.CreativeAgContactList, UpdateSourceTrigger=PropertyChanged}"

我的DataContext在我的视图中设置:

DataContact = ChaseListViewModel

所以结构是

ChaseListViewModel-CreateChaseListHelperClassList-CreativeAgContactList

我想将CreateAgContactList设置为我的ComboBox上的ItemSource

1 个答案:

答案 0 :(得分:1)

我们走了。使用单元格模板:

<dxg:GridColumn FieldName="CreativeAgContactID" Header="Creative Contact Name" Width="90" FilterPopupMode="CheckedList" AllowEditing="True">
                    <dxg:GridColumn.CellTemplate>
                        <DataTemplate>
                            <dxe:ComboBoxEdit Name="cboCreativeContact" 
                                              DisplayMember="ContactName"
                                              ValueMember="ContactID"
                                              AutoComplete="True" 
                                              EditValue="{Binding Path=ContactID, UpdateSourceTrigger=PropertyChanged}"
                                              ItemsSource="{Binding Path=Data.CreativeAgContactList, UpdateSourceTrigger=PropertyChanged}">                                    
                            </dxe:ComboBoxEdit>
                        </DataTemplate>
                    </dxg:GridColumn.CellTemplate>
                    <dxg:GridColumn.CellStyle>
                        <Style TargetType="{x:Type dxg:CellContentPresenter}">
                            <Setter Property="Background" Value="#FFE8F2F3" />
                            <Setter Property="TextBlock.Foreground" Value="Black" />
                        </Style>
                    </dxg:GridColumn.CellStyle>
                </dxg:GridColumn>