2012-07-17 2 views
2

MVVM 패턴이있는 .Net 4.0 DataGrid를 사용하고 있습니다. 사용자가 셀을 선택하고 선택한 셀의 정보를 다른 DataGrid 행 (키보드 단축키 또는 컨텍스트 메뉴 복사/붙여 넣기를 통해)에 복사 할 수있게해야합니다. SelectedItem을 통해 이것을 얻으려고했거나 SelectedItem을 CommandParameter로 보내려고했지만이 기능은 셀 전체가 아닌 전체 행에서만 작동합니다. (DataGrid는 Float 필드가있는 객체가 포함 된 ObservableCollection에 바인딩됩니다.이 필드는 DataGrid 셀에 매핑됩니다.) WPF MVVM에서 DataGrid 셀을 한 행에서 다른 행으로 복사하는 방법이 있습니까? 사전 XAML에서 감사 :선택한 셀의 MVVM DataGrid 정보 복사

<DataGrid Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="9" AutoGenerateColumns="False"  Height="Auto" HorizontalAlignment="Left" 
Name="dataGrid" VerticalAlignment="Top" Width="{Binding ElementName=grid4,Path=Width}" 
ScrollViewer.CanContentScroll="False" FrozenColumnCount="1" SelectionUnit="Cell" SelectionMode="Extended" CanUserSortColumns = "False" 
CanUserReorderColumns="False" CanUserResizeRows="False" RowHeight="25" RowBackground="LightGray" AlternatingRowBackground="White" 
ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollBarVisibility="Visible" 
ItemsSource="{Binding Layers, Mode=TwoWay}" SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Selection, Mode=TwoWay}"> 
<DataGrid.InputBindings> 
    <KeyBinding Gesture="Shift" Command="{Binding ItemHandler}" CommandParameter="{Binding ElementName=dataGrid, Path=SelectedItems}"></KeyBinding> 
</DataGrid.InputBindings> 

뷰 모델 :

private float _selection = 0.0f; 
public float Selection 
{ 
    get 
    { 
     return _selection; 
    } 
    set 
    { 
     if (_selection != value) 
     { 
      _selection = value; 
      NotifyPropertyChanged("Selection"); 
     } 
    } 
} 

...

public DelegateCommand<IList> SelectionChangedCommand = new DelegateCommand<IList>(
    items => 
    { 
     if (items == null) 
     { 
      NumberOfItemsSelected = 0; 
      return; 
     } 
     NumberOfItemsSelected = items.Count; 
    }); 
public ICommand ItemHandler 
{ 
    get 
    { 
     return SelectionChangedCommand; 
    } 
} 

답변

1

나는 당신이 SelectionUnit 재산 후있을 것 같아요. 이 값을 CellOrRowHeader으로 설정하면 선택 방법이 전체 행에서 단일 셀로 변경됩니다.

당신은 멋진 "어떤 행이 있습니까?" 강조하지만 하나의 셀에 초점을 맞추고 있습니다. (아마도 현재 행 로직을 사용하여 자신의 하이라이트를 추가하기 위해 데이터 격자를 확장 할 수 있습니다.)

<DataGrid AutoGenerateColumns="True" Grid.Column="1" Grid.Row="0" 
      HorizontalAlignment="Stretch" Name="dataGrid" VerticalAlignment="Stretch" 
      DataContext="{Binding}" ItemsSource="{Binding Path=MyDataTable}" 
      IsReadOnly="True" SelectionMode="Extended" SelectionUnit="CellOrRowHeader" />