1

私は.NetとC#を初めて使用し、バインディングと少し混乱しています。MVVMを実装し、DataGridを表示するアプリケーションがあります。私が実装したいのは、ユーザーが特定のキーの組み合わせを押すと、現在選択されているセルのコンテンツが下の行のセルにコピーされることです。DataGridのSelectedItemをViewModelプロパティにバインドしようとしましたが、更新されません。CommandParameterも機能せず、アイテム数は常に0です。そのため、ユーザーが選択したセルを抽出できず、選択したセルの内容を読み取ることができません。この問題を解決する方法やこの機能を実装する方法を誰かが提案していますか?前もって感謝します。コード: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>
</DataGrid>

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

2 に答える 2

8

編集:SelectionUnit=セルはSelectedItemでは機能しません

それは通常のmvvmの方法です:

各行に表示する最初のオブジェクトタイプ

 public class MyObject {}

コレクションと選択したアイテムを保持する2番目のビューモデル

 public class MyViewmodel
 {
    public ObservableCollection<MyObject> MyItems {get;set;}
    public MyObject MySelectedItem {get;set;}

 }

xamlデータグリッド

 <DataGrid ItemsSource="{Binding MyItems}" SelectedItem="{Binding MySelectedItem, Mode=TwoWay}"/>

それがあなたが持っているものです。行を「コピー」するコマンドを作成する場合は、新しいオブジェクトMyObjectを作成し、MySelectedItemから値をコピーしてから、新しいMyObjectをコレクションに追加できます。

しかし、多分私はあなたの質問を正しく理解していません。

于 2012-07-16T13:24:43.350 に答える
0

依存関係を適切に追加できると思います。これはプロパティの答えです:

public class UIElementMouseRightButtonDownCommandBehavior : CommandBehaviorBase<UIElement>
{
    public UIElementMouseRightButtonDownCommandBehavior(UIElement obj)
        : base(obj)
    {
        if (obj == null) throw new System.ArgumentNullException("obj");
        obj.MouseRightButtonDown += OnMouseRightButtonDown;
    }

    private void OnMouseRightButtonDown(object sender, MouseButtonEventArgs e)
    {
        ExecuteCommand();
    }       
}

そして別のクラス:

 public class MouseRightButtonDown
{
    private static readonly DependencyProperty MouseRightButtonDownCommandBehaviorProperty = DependencyProperty.RegisterAttached(
        "MouseRightButtonDownCommandBehavior",
        typeof(UIElementMouseRightButtonDownCommandBehavior),
        typeof(MouseRightButtonDown),
        null);

    public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached(
        "Command",
        typeof(ICommand),
        typeof(MouseRightButtonDown),
        new PropertyMetadata(OnSetCommandCallback));

    public static void SetCommand(UIElement element, ICommand command)
    {
        element.SetValue(CommandProperty, command);
    }

    public static ICommand GetCommand(UIElement element)
    {
        return element.GetValue(CommandProperty) as ICommand;
    }

    private static void OnSetCommandCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
    {
        UIElement element = dependencyObject as UIElement;
        if (element != null)
        {
            UIElementMouseRightButtonDownCommandBehavior behavior = GetOrCreateBehavior(element);
            behavior.Command = e.NewValue as ICommand;
        }
    }

    private static UIElementMouseRightButtonDownCommandBehavior GetOrCreateBehavior(UIElement element)
    {
        UIElementMouseRightButtonDownCommandBehavior behavior = element.GetValue(MouseRightButtonDownCommandBehaviorProperty) as UIElementMouseRightButtonDownCommandBehavior;
        if (behavior == null)
        {
            behavior = new UIElementMouseRightButtonDownCommandBehavior(element);
            element.SetValue(MouseRightButtonDownCommandBehaviorProperty, behavior);
        }
        return behavior;
    }
}

次に、ビューモデルファイルで:

public ICommand SelectNameCommand
    {
        get { return new Command(P => SelectName()); }
    }

XAMLは正しいと思います。

于 2013-09-06T02:46:54.197 に答える