トップ回答者
WPF Win8 タッチパネルでのDataGrid 複数選択

質問
回答
-
こんな?
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <Style TargetType="{x:Type DataGridCell}"> <EventSetter Event="PreviewMouseLeftButtonDown" Handler="cell_PreviewMouseLeftButtonDown" /> <EventSetter Event="PreviewTouchDown" Handler="cell_PreviewTouchDown" /> </Style> <Style TargetType="{x:Type DataGridRowHeader}"> <EventSetter Event="PreviewMouseLeftButtonDown" Handler="cell_PreviewMouseLeftButtonDown" /> <EventSetter Event="PreviewTouchDown" Handler="cell_PreviewTouchDown" /> </Style> </Window.Resources> <DataGrid ItemsSource="{Binding}" DataContext="ABCDEFGHIJKLMNOPQRSTUVWXYZ" RowHeaderWidth="50" TextElement.FontSize="30" > <DataGrid.Columns> <DataGridTextColumn Binding="{Binding}" IsReadOnly="true" Header="Test" Width="100"/> <DataGridTextColumn Binding="{Binding}" IsReadOnly="true" Header="Test" Width="100"/> </DataGrid.Columns> </DataGrid> </Window>
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; using System.Windows.Controls.Primitives; namespace WpfApplication1 { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } bool isTouch = false; private void cell_PreviewTouchDown(object sender, TouchEventArgs e) { Test(sender); e.Handled = true; isTouch = true; } void cell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { //TouchイベントをHandled=trueしてもMouseイベントも発生してしまうので //Touchイベントの後はマウスイベントの処理を阻止 e.Handled = isTouch; isTouch = false; Test(sender); } private void Test(object sender) { DataGridRowHeader header = sender as DataGridRowHeader; DataGridCell cell = sender as DataGridCell; DependencyObject d = cell ?? (DependencyObject)header; DataGridRow row = null; DataGrid dg = null; while (d != null && row == null) { row = d as DataGridRow; d = VisualTreeHelper.GetParent(d); } while (d != null && dg == null) { dg = d as DataGrid; d = VisualTreeHelper.GetParent(d); } dg.Focus(); if (cell != null && dg.SelectionUnit != DataGridSelectionUnit.FullRow) { cell.IsSelected = !row.IsSelected; cell.Focus(); } else { row.IsSelected = !row.IsSelected; row.Focus(); } } } }
個別に明示されていない限りgekkaがフォーラムに投稿したコードにはフォーラム使用条件に基づき「MICROSOFT LIMITED PUBLIC LICENSE」が適用されます。(かなり自由に使ってOK!)
- 回答としてマーク 星 睦美 2015年8月17日 1:57
すべての返信
-
こんな?
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <Style TargetType="{x:Type DataGridCell}"> <EventSetter Event="PreviewMouseLeftButtonDown" Handler="cell_PreviewMouseLeftButtonDown" /> <EventSetter Event="PreviewTouchDown" Handler="cell_PreviewTouchDown" /> </Style> <Style TargetType="{x:Type DataGridRowHeader}"> <EventSetter Event="PreviewMouseLeftButtonDown" Handler="cell_PreviewMouseLeftButtonDown" /> <EventSetter Event="PreviewTouchDown" Handler="cell_PreviewTouchDown" /> </Style> </Window.Resources> <DataGrid ItemsSource="{Binding}" DataContext="ABCDEFGHIJKLMNOPQRSTUVWXYZ" RowHeaderWidth="50" TextElement.FontSize="30" > <DataGrid.Columns> <DataGridTextColumn Binding="{Binding}" IsReadOnly="true" Header="Test" Width="100"/> <DataGridTextColumn Binding="{Binding}" IsReadOnly="true" Header="Test" Width="100"/> </DataGrid.Columns> </DataGrid> </Window>
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; using System.Windows.Controls.Primitives; namespace WpfApplication1 { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } bool isTouch = false; private void cell_PreviewTouchDown(object sender, TouchEventArgs e) { Test(sender); e.Handled = true; isTouch = true; } void cell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { //TouchイベントをHandled=trueしてもMouseイベントも発生してしまうので //Touchイベントの後はマウスイベントの処理を阻止 e.Handled = isTouch; isTouch = false; Test(sender); } private void Test(object sender) { DataGridRowHeader header = sender as DataGridRowHeader; DataGridCell cell = sender as DataGridCell; DependencyObject d = cell ?? (DependencyObject)header; DataGridRow row = null; DataGrid dg = null; while (d != null && row == null) { row = d as DataGridRow; d = VisualTreeHelper.GetParent(d); } while (d != null && dg == null) { dg = d as DataGrid; d = VisualTreeHelper.GetParent(d); } dg.Focus(); if (cell != null && dg.SelectionUnit != DataGridSelectionUnit.FullRow) { cell.IsSelected = !row.IsSelected; cell.Focus(); } else { row.IsSelected = !row.IsSelected; row.Focus(); } } } }
個別に明示されていない限りgekkaがフォーラムに投稿したコードにはフォーラム使用条件に基づき「MICROSOFT LIMITED PUBLIC LICENSE」が適用されます。(かなり自由に使ってOK!)
- 回答としてマーク 星 睦美 2015年8月17日 1:57