我有一个包含一些数据的数据网格,当我单击一个按钮时,应该将行向上或向下移动(取决于按钮),这是我面临的问题。当我单击移动时,可以说下来,第一个索引移至索引2,索引2移至索引1,UI显示更新后的列表,但是当我单击第一行时,第二行被选择为相同如果单击第二行,则选择第一行。下面是我的代码:
数据网格:
<dg:DataGrid x:Name="data_grid"
ItemsSource="{Binding obs_plano_descarga}"
SelectionEnabled="True"
RowHeight="50"
HeaderHeight="30"
BorderColor="#CCCCCC"
HeaderBackground="#5963f7"
ActiveRowColor="#8899AA" ItemSelected="Data_grid_ItemSelected"
Grid.Column="0" Grid.Row="1">
<dg:DataGrid.Resources>
<ResourceDictionary>
<conv:data_grid_converter x:Key="StreakToColorConverter" />
</ResourceDictionary>
</dg:DataGrid.Resources>
<dg:DataGrid.RowsBackgroundColorPalette>
<dg:PaletteCollection>
<Color>#F2F2F2</Color>
<Color>#FFFFFF</Color>
</dg:PaletteCollection>
</dg:DataGrid.RowsBackgroundColorPalette>
<dg:DataGrid.Columns>
<dg:DataGridColumn Title="Comp."
PropertyName="compartimento"
Width="2*"
VerticalContentAlignment="CenterAndExpand"
HorizontalContentAlignment="CenterAndExpand" />
<dg:DataGridColumn Title="Prod"
PropertyName="codigo_produto"
Width="2*"
VerticalContentAlignment="CenterAndExpand"
HorizontalContentAlignment="CenterAndExpand" />
<dg:DataGridColumn Title="Volume"
PropertyName="volume"
Width="3*"
VerticalContentAlignment="CenterAndExpand"
HorizontalContentAlignment="CenterAndExpand" />
<dg:DataGridColumn Title="Posto"
PropertyName="descricao_posto"
Width="5*"
VerticalContentAlignment="CenterAndExpand"
HorizontalContentAlignment="CenterAndExpand" />
<dg:DataGridColumn Title="tqe"
PropertyName="seq_tanque"
Width="2*"
VerticalContentAlignment="CenterAndExpand"
HorizontalContentAlignment="CenterAndExpand" />
</dg:DataGrid.Columns>
</dg:DataGrid>
* ItemSelected:*
private void Data_grid_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
plano_select = (plano_carga_descarga)e.SelectedItem;
index = e.SelectedItemIndex;
}
将行移至下一个索引:
private void Tap_baixo_Tapped(object sender, EventArgs e)
{
lista_plano.AddRange(view_model_plano_descarga.local_plano_descarga);
if (plano_select != null && index > -1)
{
var index_plano = lista_plano.IndexOf(plano_select);
lista_plano.Remove(plano_select);
lista_plano.Insert(index_plano + 1, plano_select);
data_grid.SelectedItem = null;
BindingContext = new view_model_plano_descarga(lista_plano);
}
}
view_model:
public view_model_plano_descarga(List<plano_carga_descarga> plano)
{
if (plano != null)
{
obs_plano_descarga.Clear();
for (int i = 0; i < plano.Count; i++)
{
local_plano_descarga.Add(plano[i]);
}
local_dados_motorista.Add(local_plano_descarga[0]);
model_turno turno = new model_turno();
turno.muda_background(local_plano_descarga[0].turno);
}
}
public static ObservableCollection<plano_carga_descarga> local_plano_descarga = new ObservableCollection<plano_carga_descarga>();
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string property)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}