3

MVVMパターンで.Net4.0DataGridを使用しています。ユーザーがセルを選択し、選択したセルから他のDataGrid行に情報をコピーできるようにする必要があります(キーボードショートカットまたはコンテキストメニューのコピー/貼り付けを使用)。SelectedItemを介して、またはSelectedItemをCommandParameterとして送信してこれを達成しようとしましたが、これは行全体でのみ機能し、セルでは機能しません。(DataGridは、floatフィールドを持つオブジェクトを含むObservableCollectionにバインドされます。これらのフィールドはDataGridセルにマップされます)では、DataGridセルをある行から別の行にコピーする方法はWPF MVVMにありますか?事前にありがとうxaml:

    <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>

ViewModel:

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;
    }
}
4

1 に答える 1

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" />
于 2012-11-20T23:58:15.430 に答える