赞
踩
XMAL文件中:
</Grid>
<DataGrid Grid.Row="0" x:Name="PersonDataGrid">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Height" Value="26" />
<Setter Property="FontSize" Value="13"/>
</Style>
</DataGrid.RowStyle>
<DataGrid.ColumnHeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
<Setter Property="Background" Value="#B0E0E6"></Setter>
<Setter Property="Foreground" Value="#17acae"></Setter>
<Setter Property="BorderThickness" Value="1" />
<Setter Property="BorderBrush" Value="#17acae" />
<Setter Property="Height" Value="26" />
<Setter Property="FontSize" Value="16"/>
</Style>
</DataGrid.ColumnHeaderStyle>
<DataGrid.Columns>
<DataGridTemplateColumn Width="40" Header="序号" Visibility="Collapsed" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type DataGridRow}}, Path=Header}" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="10,0,0,0"></TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Binding="{Binding Name}" Header="名称" Width="130" />
<DataGridTextColumn Binding="{Binding Age}" Header="年龄" Width="130" />
<DataGridTextColumn Binding="{Binding Sex}" Header="性别" Width="130" />
<DataGridTextColumn Binding="{Binding Mark}" Header="备 注" Width="150" />
</DataGrid.Columns>
<DataGrid.ContextMenu>
<ContextMenu>
<Menu Name="up" PreviewMouseLeftButtonDown="up_PreviewMouseLeftButtonDown">上移</Menu>
<Menu Name="down" PreviewMouseLeftButtonDown="down_PreviewMouseLeftButtonDown">下移</Menu>
</ContextMenu>
</DataGrid.ContextMenu>
</DataGrid>
</Grid>
cs文件中: 存放数据 ObservableCollection 及两个事件
//给表格赋值 : PersonDataGrid.ItemsSource = Userinfos;
private ObservableCollection<UerInfo> userinfos = new ObservableCollection<UerInfo>();
public ObservableCollection<UerInfo> Userinfos
{
get { return userinfos; }
set { userinfos = value; }
}
/// <summary>
/// 上移
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void up_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
UerInfo selData = PersonDataGrid.SelectedItem as UerInfo;
int index = Userinfos.IndexOf(selData);
if (index == 0)
return;
Userinfos.RemoveAt(index);
index = index - 1;
Userinfos.Insert(index, selData);
}
/// <summary>
/// 下移
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void down_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
UerInfo selData = PersonDataGrid.SelectedItem as UerInfo;
int index = Userinfos.IndexOf(selData);
if (index == PersonDataGrid.Items.Count-1)
return;
Userinfos.RemoveAt(index);
index = index + 1;
Userinfos.Insert(index, selData);
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。