8,737
社区成员
发帖
与我相关
我的任务
分享
<Window.Resources>
<local:DataGridTextCellValueConverter x:Key="converter"/>
</Window.Resources>
<Window.DataContext>
<local:MainViewModel/>
</Window.DataContext>
<DataGrid ItemsSource="{Binding Persons}"
SelectedItem="{Binding CurrentRow}"
CurrentCell="{Binding CurrentText, Mode=OneWayToSource, Converter={StaticResource converter}}"
IsReadOnly="True"
SelectionMode="Single">
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Header="Copy" Command="{Binding CopyCommand}" CommandParameter="{Binding CurrentText}"/>
</ContextMenu>
</DataGrid.ContextMenu>
</DataGrid>
public class DataGridTextCellValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
var cellInfo = (DataGridCellInfo)value;
if (cellInfo==null || cellInfo.Column == null || cellInfo.Item == null) return string.Empty;
var cellControl = cellInfo.Column.GetCellContent(cellInfo.Item);
var textBlock = cellControl as TextBlock;
if (textBlock == null) return string.Empty;
return textBlock.Text;
}
}
public class MainViewModel
{
public MainViewModel()
{
Persons = new ObservableCollection<PersonViewModel>();
Persons.Add(new PersonViewModel { Name = "ssss", Age = 12 });
Persons.Add(new PersonViewModel { Name = "xxxx", Age = 30 });
}
public ObservableCollection<PersonViewModel> Persons { get; set; }
private PersonViewModel currentRow;
public PersonViewModel CurrentRow
{
get
{
return currentRow;
}
set
{
currentRow = value;
if (currentRow != null)
{
Console.WriteLine("CurrentRow: Name = {0}, Age = {1} ",
currentRow.Name, currentRow.Age);
}
}
}
private string currentText;
public string CurrentText
{
get
{
return currentText;
}
set
{
currentText = value;
Console.WriteLine("CurrentText: " + currentText);
}
}
private ICommand _copyCommand;
public ICommand CopyCommand
{
get
{
if (_copyCommand == null)
{
_copyCommand = new RelayCommand<string>(OnCopyAction);
}
return _copyCommand;
}
}
private void OnCopyAction(string param)
{
if (string.IsNullOrWhiteSpace(param)) return;
Clipboard.Clear();
Clipboard.SetDataObject(param);
}
}
private static DependencyObject VisualUpwardSearch<M>(DependencyObject source)
{
while (source != null && source.GetType() != typeof(M))
{
if (source is Visual || source is Visual3D)
source = VisualTreeHelper.GetParent(source);
else
source = LogicalTreeHelper.GetParent(source);
}
return source;
}
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseRightButtonDown">
<command:EventToCommand Command="{Binding DataContext.SelectedCellItemCommand,
RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" PassEventArgsToCommand="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
private readonly ICommand _selectCellItemCommand = null;
public ICommand SelectedCellItemCommand
{
get { return _selectCellItemCommand.Relay<RoutedEventArgs>(SelectedCellItemCmdAction); }
}
private void SelectedCellItemCmdAction(RoutedEventArgs e)
{
var dataGridCell = VisualUpwardSearch<DataGridCell>(e.OriginalSource as DependencyObject) as DataGridCell;
if (dataGridCell == null) return;
dataGridCell.Focus();
e.Handled = true;
}