LoginSignup
0
1

More than 5 years have passed since last update.

WPF > 実装: DataGridの行を選択し、ダブルクリックした時にそのレコードを取得する (一部失敗) | "/"を使ってcurrentを取得

Last updated at Posted at 2017-07-02
動作環境
Windows 8.1 Pro (64bit)
Microsoft Visual Studio 2017 Community

概要

以下をもくろんでいる

  • ICommand使用
  • DataGridの行を選択
  • ダブルクリック時に選択した行の情報を表示

参考

https://stackoverflow.com/questions/3876662/wpf-datagrid-commandbinding-to-a-double-click-instead-of-using-events

answered Nov 6 '13 at 9:32
Mizipzor
を参考にしました。

"/"を使う点がみそのようです。

is the / in your {Binding CollectionView/} on purpose? – Maslow Sep 23 '15 at 17:41

Yes, that makes it bind to the current item. Which when used with IsSynchronizedWithCurrentItem means the selected item. Here's a blog post. – Mizipzor Oct 1 '15 at 9:48

code

MainWindow.xaml
<Window x:Class="_170702_t1630_doubleClickBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:_170702_t1630_doubleClickBinding"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <local:ViewModel/>
    </Window.DataContext>
    <Grid>
        <DataGrid x:Name="dataGrid1" 
                  ItemsSource="{Binding myList}"
                  IsSynchronizedWithCurrentItem="True">
            <DataGrid.InputBindings>
                <MouseBinding MouseAction="LeftDoubleClick"
                              Command="{Binding DoubleClickCommand}"
                              CommandParameter="{Binding myList/}"/>
            </DataGrid.InputBindings>
        </DataGrid>
    </Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace _170702_t1630_doubleClickBinding
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}
TestItem.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _170702_t1630_doubleClickBinding
{
    class TestItem
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public string Gender { get; set; }
    }
}
ViewModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//
using System.Collections.ObjectModel;
using System.Windows.Input;
using System.Windows;
using System.Data;
using System.Data.SqlClient;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace _170702_t1630_doubleClickBinding
{
    class ViewModel : ViewModelBase
    {
        // コンストラクタ
        public ViewModel()
        {
            DoubleClickCommand = CreateCommand(param => MyDoubleClickCommand(param));
            //
            myList = new ObservableCollection<TestItem>();
            myList.Add( new TestItem { Id = 1, Name = "7of9", Age = 30, Gender = "Female"});
            myList.Add( new TestItem { Id = 2, Name = "Janeway", Age = 50, Gender = "Female" });
            myList.Add( new TestItem { Id = 3, Name = "Chakotay", Age = 40, Gender = "Female" });
        }

        public ObservableCollection<TestItem> myList { get; set; }

        public ICommand DoubleClickCommand { get; private set; }

        public void MyDoubleClickCommand(object param)
        {
            if (param == null)
            {
                return;
            }
            var clc = (TestItem)param;
            MessageBox.Show("Double click on " + clc.Name);
        }

    }
}

ViewModelBase.csは@hugo-sbさんのコードを使わせていただいています。
ありがとうございます。

実行

  1. Chakotayの行を選択する
  2. その右側をダブルクリックする 2017-07-02_17h02_36.png

Chakotayの表示がされる。
2017-07-02_17h03_07.png

本当は違うんだ日記

下記の点でコレジャナイ。

  • 項目が記載されている左側をダブルクリックしたい
    • でもそうすると、編集画面になる
    • Readonlyにすればいいだろうか
  • 行を選択せずに、いきなりダブルクリックしたい
    • 行選択しないと7of9になる

IsReadOnly="True"

DataGridのIsReadOnlyをTrueにしてみた。

行を選択せずに、項目が記載されている列をダブルクリックしたら、その行の項目が表示される。

項目が記載されていない右側の列のダブルクリックでは7of9になる。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1