其中XMAL文件中的表格样式:
<DataGrid x:Name="DataListView" AutoGenerateColumns="False" HorizontalAlignment="Center" Margin="14,10,162,0" VerticalAlignment="Top" ItemsSource="{Binding }" CanUserAddRows="False" CanUserSortColumns="False" CanUserDeleteRows="False" Width="350" Height="239" SelectionMode="Extended" SelectionUnit="Cell">
<!-- This is required to handle CTRL + C when something is selected in the DataGrid -->
<DataGrid.CommandBindings>
<CommandBinding Command="Paste" Executed="PasteCommand" />
</DataGrid.CommandBindings>
<DataGrid.InputBindings>
<KeyBinding Key="V" Modifiers="Control" Command="Paste" />
</DataGrid.InputBindings>
<DataGrid.ColumnHeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="White" Offset="0"/>
<GradientStop Color="Green" Offset="0.5"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="FontSize" Value="16" />
</Style>
</DataGrid.ColumnHeaderStyle>
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers >
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Snow"/>
<Setter Property="Foreground" Value="LimeGreen"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
<DataGrid.Columns>
<!--UpdateSourceTrigger=PropertyChanged-->
<DataGridTextColumn Header="模块名称" Binding="{Binding Path=strModuleName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" MinWidth="80"/>
<DataGridTextColumn Header="最小值" Binding="{Binding Path=MinValue,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" MinWidth="80" />
<DataGridTextColumn Header="最大值" Binding="{Binding Path=MaxValue,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" MinWidth="80" />
<DataGridTextColumn Header="单位" Binding="{Binding Path=strUnit,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" MinWidth="80" />
</DataGrid.Columns>
</DataGrid>
使用MVVM模式下的双向绑定,红色部分需要注意
后台代码如下:
/// <summary>
/// 粘贴处理剪贴板
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void PasteCommand(object sender, ExecutedRoutedEventArgs e)
{
// 获取剪切板的内容,并按行分割
string pasteText = Clipboard.GetText();
if (string.IsNullOrEmpty(pasteText))
return;
int cIndex = -1;
int rIndex = -1;
if (DataListView.SelectedCells.Count > 0)
{
DataGridCellInfo dgcInfo = DataListView.SelectedCells[0];
cIndex = dgcInfo.Column.DisplayIndex;
Dinfo dii = dgcInfo.Item as Dinfo;
rIndex = GetDinfoIndex(dii);
Console.WriteLine("("+ cIndex.ToString() + "," + rIndex.ToString() + ")");
}
string[] Rinfo = pasteText.Split(new string[]{"\r\n"}, StringSplitOptions.RemoveEmptyEntries);
if (cIndex > -1 && rIndex>-1)
{
int rSum = Math.Min(Rinfo.Length, DataListView.Items.Count - rIndex);
for(int i=0;i<rSum;i++ )
{
string[] Cinfo = Rinfo[i].Split(new string[] { "\t" }, StringSplitOptions.RemoveEmptyEntries);
int cSum = Math.Min(Cinfo.Length, DataListView.Columns.Count - cIndex);
for(int j=0;j<cSum;j++)
{
//Console.WriteLine("行:" + (rIndex + i).ToString());
//Console.WriteLine("列:" + (cIndex + j).ToString());
DataGridColumn dgcC = DataListView.Columns[cIndex + j];
//Dinfo dinfo = DataListView.Items[rIndex + i] as Dinfo;
try
{
(DataListView.Columns[cIndex + j].GetCellContent(DataListView.Items[rIndex + i]) as TextBlock).Text = Cinfo[j];
}
catch
{
}
}
}
//DataListView.Items.Refresh();
}
Console.WriteLine(pasteText);
}
private int GetDinfoIndex(Dinfo di)
{
if( displaydatas.Contains(di))
{
return displaydatas.IndexOf(di);
}
else
{
return -1;
}
}
模拟MVVM加载数据
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Random rd = new Random();
for(int i=0;i<5;i++)
{
Dinfo di = new Dinfo();
di.MaxValue = 100 + rd.Next(0, 50);
di.MinValue = 10 + rd.Next(1, 80);
di.strModuleName = "模型" + i.ToString();
di.strUnit =(new[] {"C","%","-","dB","Km","Kg","mm","cm","#"})[rd.Next(0,8)];
displaydatas.Add(di);
}
DataListView.ItemsSource = displaydatas;
listView.Items.Clear();
listView.ItemsSource = displaydatas;
}
模型类:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfAppShow
{
public class Dinfo: INotifyPropertyChanged
{
private string strmodulename;
private string strunit;
private int maxValue;
private int minValue;
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
public string strModuleName
{
get { return strmodulename; }
set
{
strmodulename = value;
OnPropertyChanged("strModuleName");
}
}
public string strUnit
{
get { return strunit; }
set
{
strunit = value;
OnPropertyChanged("strUnit");
}
}
public int MaxValue
{
get { return maxValue; }
set
{
maxValue = value;
OnPropertyChanged("MaxValue");
}
}
public int MinValue
{
get { return minValue; }
set
{
minValue = value;
OnPropertyChanged("MinValue");
}
}
}
}