动态添加复选框

时间:2013-07-01 02:52:01

标签: c# .net wpf devexpress devexpress-windows-ui

我已实施GRIDCONTROL,其中添加了一个TABLEVIEW,其中包含NAME, AGE, SALARY列。我从数据库中获取数据并相应地填充列。现在我想为每一行添加一个Checkbox,这样我就可以一次选择两行并获取相应的数据。如何实现Checkbox?

我正在使用DEVEXPRESS控件

<GridControl Name="gridControl1">
    <GridControl.View>
        <TableView x:Name="view" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  />
    </GridControl.View>
    <GridControl.Columns>
        <GridColumn FieldName="Name"  Name="Name"  Width="120" Header="Name"/>
        <GridColumn FieldName="Age"  Name="Age"  Width="120" Header="Age"/>                
        <GridColumn FieldName="Salary"  Name="Salary"  Width="120" Header="Salary" />                
    <GridControl.Columns>
<GridControl>

现在我想为行的每个条目添加Checkbox,所以如果选中该复选框,我可以获取与该行对应的所有数据。

1 个答案:

答案 0 :(得分:0)

我正在将此方法用于类似的应用程序。单独的列用于选择人员。该列的标题中还有一个复选框,用于选择/取消选择所有人。

视图模型

private ObservableCollection<PersonModel> _peopleCollection;
public ObservableCollection<PersonModel> PeopleCollection
{
        get { return _peopleCollection; }
        set
        {
            _peopleCollection = value;
            RaisePropertyChanged("PeopleCollection");
        }
    }

private void initPeople()
    {
        foreach (var person in PeopleCollection)
        {             
            person.PropertyChanged += PersonOnPropertyChanged;
       }
    }

private void PersonOnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs)
    {
        if(propertyChangedEventArgs.PropertyName == "IsPersonSelected")
        {
    //Handle checked/unchecked person

        }
    }


private bool _allPersonsSelected
    public bool AllPersonsSelected
    {
        get { return _allPersonsSelected; }
        set
        {
            _allPersonsSelected= value;
            RaisePropertyChanged("AllPersonsSelected");

            if (value)
                SelectRows();
            else
            {
                DeselectRows();
            }
        }
    }

    public void SelectRows()
    {
        foreach (var record in PeopleCollection)
            record.IsPersonSelected = true;
    }

    public void DeselectRows()
    {
        foreach (var record in PeopleCollection)
            record.IsPersonSelected = false;
    }

}

以下是视图中的网格xaml

<dxg:GridControl ItemsSource="{Binding Path=PeopleCollection}">
            <dxg:GridControl.View>
                <dxg:TableView>
                </dxg:TableView>
            </dxg:GridControl.View>
            <dxg:GridControl.Columns>
                <dxg:GridColumn FieldName="IsPersonSelected"
                                HorizontalHeaderContentAlignment="Left">
                    <dxg:GridColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock>Selected</TextBlock>
                            <dxe:CheckEdit EditValue="{Binding Path=AllPersonsSelected}"
                                           Margin="10,0,0,0" />
                        </StackPanel>
                    </dxg:GridColumn.Header>
                </dxg:GridColumn>
                <dxg:GridColumn FieldName="Name" Header="Name"/>
                <dxg:GridColumn FieldName="Age" Header="Age"/>
            </dxg:GridControl.Columns>
        </dxg:GridControl>