首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ComboBox中填充xamDataGrid

在ComboBox中填充xamDataGrid
EN

Stack Overflow用户
提问于 2015-08-25 10:55:02
回答 1查看 2.2K关注 0票数 0

我有一个xamDatagrid设置如下:

代码语言:javascript
复制
 <igDP:XamDataGrid  DataSource="{Binding}"  x:Name="xamContact" >
                                <igDP:XamDataGrid.FieldLayouts>
                                    <igDP:FieldLayout>
                                        <igDP:Field Name="DepartmentName" Label="Department Name" />
                                        <igDP:Field Name="FirstName" Width="100" Label="First Name" />
                                        <igDP:Field Name="LastName" Label="Last Name" />
                                        <igDP:Field Name="RoleName" Label="Role Name" />
                                       <igDP:ComboBoxField Name="Contactby" Visibility="Visible" ItemsSource="{Binding}"  Label="Contactby"></igDP:ComboBoxField>
                                        <igDP:Field  Name="ContactType" Label="ContactType" Visibility="Collapsed" />
                                   </igDP:FieldLayout>
                                </igDP:XamDataGrid.FieldLayouts>

在后面的代码中,我希望填充xamDataGrid包括ComboBox字段“Contactby”(位于xamDataGrid中)如下:

代码语言:javascript
复制
 public DataTable GetContTable()
 {

        DataTable tableCont = new DataTable();
        tableCont.Columns.Add("DepartmentName", typeof(string));
        tableCont.Columns.Add("FirstName", typeof(string));
        tableCont.Columns.Add("LastName", typeof(string));
        tableCont.Columns.Add("RoleName", typeof(string));
        tableCont.Columns.Add("Contactby", typeof(object));
        tableCont.Columns.Add("ContactType", typeof(string));
      tableCont.Columns[5].ColumnMapping = MappingType.Hidden;
        return tableCont;
 }

 public void AddRowsCont(DataTable dtcont)
 {

        string[] card = new string[1000];
        for (int i = 0; i < lstContactBy.Items.Count; i++)// in this listbox the number of items is dinamic then can change number each time insert a new record 
        {
            card[i] = lstContactBy.Items[i].ToString();
        }
        dtcont.Rows.Add(txtDepartment.Text, txtFirstName.Text, txtlastName.Text, txtROlename.Text,card  ,"SKYPE"); 

 }

    //here is the button to insert the data in the xamfdatagrid

            AddRowsCont(tableCont);
            xamContact.DataSource = tableCont.DefaultView;

其结果是xamdatagrid可以被填充,ComboBox "Contactby“只填充一个消息‘String[] Array’,我如何填充ComboBox而不产生任何错误?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-31 11:17:24

First我认为如果你定义一个模型类并在它的基础上工作,比如说一个类Item,它会更优雅

代码语言:javascript
复制
public class Item
{
    public String DepartmentName { get; set; }
    public String FirstName { get; set; }
    public String LastName { get; set; }
    public String RoleName { get; set; }
    public String Contactby { get; set; }
    public String ContactType { get; set; }
}

Secondly将您的DataGrid DataSource绑定到ObservableCollection并实现INotifyChangedInterface,这样您就不必担心每次在集合上的项或它自己更改的集合中更新DataGrid,所以在后面的代码中(或ViewModel):

代码语言:javascript
复制
private ObservableCollection<Item> _listDg = new ObservableCollection<Item>();
    public ObservableCollection<Item> ListDg
    {
        get
        {
            return _listDg;
        }

        set
        {
            if (_listDg == value)
            {
                return;
            }

            _listDg = value;
            OnPropertyChanged();
        }
    }

现在,对于您的ComboBoxField中的DataGrid,处理它的最好方法是:

1.创建另一个包含ComboBoxField项的集合,您也可以将该集合绑定到另一个listView,以便每次向列表中添加项时,ComboBoxField上的集合也将被更新,

代码语言:javascript
复制
private ObservableCollection<String> _contactByCollection = new ObservableCollection<string>()
    {
        "One","Two","Three"   // you can Bind that collection to a list or add their items programaticlly
    };
    public ObservableCollection<String> ContactByCollection
    {
        get
        {
            return _contactByCollection;
        }

        set
        {
            if (_contactByCollection == value)
            {
                return;
            }

            _contactByCollection = value;
            OnPropertyChanged();
        }
    }

2.将该集合绑定到CollectionViewSource,并使用它设置comboBox ItemSource

代码语言:javascript
复制
<Window.Resources>
    <CollectionViewSource x:Key="list" Source="{Binding ContactByCollection}"/>
</Window.Resources>

最后,,设置DataGrid DataSource和您的页面DataContext

代码语言:javascript
复制
<Window 
     ...
   DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Window.Resources>
    <CollectionViewSource x:Key="list" Source="{Binding ContactByCollection}"/>
</Window.Resources>
<Grid>        
    <igDP:XamDataGrid  DataSource="{Binding ListDg}"  x:Name="xamContact" BindToSampleData="True" IsSynchronizedWithCurrentItem="False" IsNestedDataDisplayEnabled="True" >
        <igDP:XamDataGrid.ViewSettings>
            <igDP:GridViewSettings/>
        </igDP:XamDataGrid.ViewSettings>
        <igDP:XamDataGrid.FieldLayouts>
            <igDP:FieldLayout>
                <igDP:Field Name="DepartmentName" Label="Department Name" />
                <igDP:Field Name="FirstName" Width="100" Label="First Name" />
                <igDP:Field Name="LastName" Label="Last Name" />
                <igDP:Field Name="RoleName" Label="Role Name" />
                <igDP:ComboBoxField  Name="Contactby"  ItemsSource="{Binding Source={StaticResource list}}"  Visibility="Visible"  Label="Contact by">                        
                </igDP:ComboBoxField>
                <igDP:Field  Name="ContactType" Label="ContactType" Visibility="Collapsed" />
            </igDP:FieldLayout>
        </igDP:XamDataGrid.FieldLayouts>
    </igDP:XamDataGrid>
</Grid>

这里有一个更详细的示例,其中包含ContactBy项在ListView

代码语言:javascript
复制
        Title="MainWindow" Height="350" Width="525" DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Window.Resources>
    <CollectionViewSource x:Key="list" Source="{Binding ContactByCollection}"/>
</Window.Resources>
<Grid> 
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>            
    </Grid.ColumnDefinitions>
    <StackPanel Grid.Column="0">
        <Button Click="ButtonBase_OnClick" Content="Add new ContactBy Item"/>
        <ListView ItemsSource="{Binding ContactByCollection}" Margin="3">                
        </ListView>            
    </StackPanel>
    <StackPanel Grid.Column="1">
        <igDP:XamDataGrid   DataSource="{Binding ListDg}"  x:Name="xamContact" BindToSampleData="True" IsSynchronizedWithCurrentItem="False" IsNestedDataDisplayEnabled="True" >
            <igDP:XamDataGrid.ViewSettings>
                <igDP:GridViewSettings/>
            </igDP:XamDataGrid.ViewSettings>
            <igDP:XamDataGrid.FieldLayouts>
                <igDP:FieldLayout>
                    <igDP:Field Name="DepartmentName" Label="Department Name" />
                    <igDP:Field Name="FirstName" Width="100" Label="First Name" />
                    <igDP:Field Name="LastName" Label="Last Name" />
                    <igDP:Field Name="RoleName" Label="Role Name" />
                    <igDP:ComboBoxField  Name="Contactby"  ItemsSource="{Binding Source={StaticResource list}}"  Visibility="Visible"  Label="Contact by">
                    </igDP:ComboBoxField>
                    <igDP:Field  Name="ContactType" Label="ContactType" Visibility="Collapsed" />
                </igDP:FieldLayout>
            </igDP:XamDataGrid.FieldLayouts>
        </igDP:XamDataGrid>
        <Button Content="Add an Item To the Grid" Click="AddToGrid_Click"></Button>
    </StackPanel>

</Grid>

以及代码隐藏

代码语言:javascript
复制
public class Item
{
    public String DepartmentName { get; set; }
    public String FirstName { get; set; }
    public String LastName { get; set; }
    public String RoleName { get; set; }
    public String Contactby { get; set; }
    public String ContactType { get; set; }
}
public partial class MainWindow : Window, INotifyPropertyChanged
{       
    private ObservableCollection<String> _contactByCollection = new ObservableCollection<string>()
    {
        "One","Two","Three"   // you can Bind that collection to a list or add their items programaticlly
    };
    public ObservableCollection<String> ContactByCollection
    {
        get
        {
            return _contactByCollection;
        }

        set
        {
            if (_contactByCollection == value)
            {
                return;
            }

            _contactByCollection = value;
            OnPropertyChanged();
        }
    }

    private ObservableCollection<Item> _listDg = new ObservableCollection<Item>();
    public ObservableCollection<Item> ListDg
    {
        get
        {
            return _listDg;
        }

        set
        {
            if (_listDg == value)
            {
                return;
            }

            _listDg = value;
            OnPropertyChanged();
        }
    }
    public MainWindow()
    {
        InitializeComponent();            
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }

    private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        ContactByCollection.Add("NewContactByItem");
    }

    private void AddToGrid_Click(object sender, RoutedEventArgs e)
    {
        ListDg.Add(new Item()
        {
            FirstName = "FirstName" //Other properties
            ,Contactby = "Three" //..
        });
    }
}

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32202163

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档