简体   繁体   English

Xamarin形成devexpress网格-访问单元

[英]Xamarin Forms devexpress grid - access cell

I'm using Xamarin Forms with devexpress grid, which contains multiple rows. 我正在将Xamarin Forms与devexpress网格一起使用,该网格包含多行。 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). material是一个简单的List,其中Material是一个简单的示例类,其中包含两个字符串属性(id,description)。

As you can see in the documentation , you can bind the property SelectedRowHandle to recover the selected row: 如您在文档中所见,可以绑定属性SelectedRowHandle来恢复选定的行:

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. 使用SelectedRowHandle属性获取或设置网格中当前选定的行。 The SelectedDataObject property returns an object that specifies a data source record to which the row selected in the grid corresponds. SelectedDataObject属性返回一个对象,该对象指定网格中所选行所对应的数据源记录。 After the grid's selection is changed, the SelectionChanged event occurs. 更改网格的选择后,发生SelectionChanged事件。

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 "-";
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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