简体   繁体   中英

Xamarin Forms devexpress grid - access cell

I'm using Xamarin Forms with devexpress grid, which contains multiple rows. I'd like to be able to get the value of a selected row / cell. How can I achieve this?

<ScrollView>
    <dxGrid:GridControl 
        x:Name="grid" 
        ItemsSource="{Binding materials}"
        AutoFilterPanelVisibility="true"
        IsReadOnly="true"
        SortMode="Multiple">
            <dxGrid:GridControl.Columns>
                <dxGrid:TextColumn 
                    FieldName="id" 
                    Caption = "ID" 
                    AutoFilterCondition="Contains"/>
                <dxGrid:TextColumn 
                    FieldName="description" 
                    Caption = "Material" 
                    AutoFilterCondition="Contains"/>
            </dxGrid:GridControl.Columns>
    </dxGrid:GridControl>
</ScrollView>

materials is a simple List, where Material is a simple example class, containing two string properties (id, description).

As you can see in the documentation , you can bind the property SelectedRowHandle to recover the selected row:

When an end-user taps a data row in the grid, this row becomes selected. Use the SelectedRowHandle property to obtain or set a row currently selected in the grid. The SelectedDataObject property returns an object that specifies a data source record to which the row selected in the grid corresponds. After the grid's selection is changed, the SelectionChanged event occurs.

I hope this can help you.

Figured it out myself:

<ScrollView>
    <dxGrid:GridControl 
        x:Name="grid" 
        ItemsSource="{Binding materials}"
        AutoFilterPanelVisibility="true"
        IsReadOnly="true"
        SortMode="Multiple"
        SelectedRowHandle="{Binding selectedRow, Mode=TwoWay}"
        SelectedDataObject="{Binding selectedRowObject, Mode=TwoWay}">
        <dxGrid:GridControl.Columns>
            <dxGrid:TextColumn 
                FieldName="description" 
                Caption = "Material" 
                AutoFilterCondition="Contains"/>
            </dxGrid:GridControl.Columns>
        </dxGrid:GridControl>
    </ScrollView>

And the viewmodel:

private int SelectedRow;
public int selectedRow
    {
        set
        {
            if(SelectedRow != value)
            {
                SelectedRow = value;
                if(PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("selectedRow"));
                }
            }
        }
        get
        {
            return SelectedRow;
        }
    }

    private Object SelectedRowObject;
    public Object selectedRowObject
    {
        set
        {
            if (SelectedRowObject != value)
            {
                SelectedRowObject = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("selectedRowObject"));
                    PropertyChanged(this, new PropertyChangedEventArgs("selectedRowDescription"));
                }
            }
        }
        get
        {
            return SelectedRowObject;
        }
    }

    private String SelectedRowDescription;
    public String selectedRowDescription
    {
        get
        {
            if(SelectedRowObject != null && SelectedRowObject is Material)
            {
                Material mat = (Material)SelectedRowObject;
                return mat.description;
            } else
            {
                return "-";
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM