我正在使用带有devexpress网格的Xamarin Forms,其中包含多行。 我希望能够获取所选行/单元格的值。我怎样才能做到这一点?
<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>
材料是一个简单的列表,其中材料是一个简单的示例类,包含两个字符串属性(id,description(。
正如您在文档中看到的,您可以绑定属性SelectedRowHandle
以恢复所选行:
当最终用户点击网格中的数据行时,此行将被选中。使用 SelectedRowHandle 属性可以获取或设置网格中当前选定的行。属性返回一个对象,该对象指定网格中选择的行对应于的数据源记录。更改网格的选择后,将发生 SelectionChanged 事件。
我希望这能帮助你。
自己想通了:
<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>
和视图模型:
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 "-";
}
}
}