- 追加された行はこの色です。
- 削除された行はこの色です。
&tag(WPF/DataGrid/Tips);
*目次 [#u0c0a459]
#contents
*参考情報 [#w7e00b42]
-[[WPF/DataGrid]]
*Tips [#ccab84b4]
**DataGridRow、DataGridCellを検索する [#o3493e81]
-[[Techie things: Get WPF DataGrid row and cell:http://techiethings.blogspot.com/2010/05/get-wpf-datagrid-row-and-cell.html]]
**列を動的に生成する [#z7f7562c]
-[[c# - How can I programatically create a WPF Toolkit DataGridTemplateColumn? - Stack Overflow:http://stackoverflow.com/questions/1757339/how-can-i-programatically-create-a-wpf-toolkit-datagridtemplatecolumn]]にあるようにFrameworkElementFactoryを使って自力で作る。
-[[wpf - dynamic datatemplate with valueconverter - Stack Overflow:http://stackoverflow.com/questions/652304/dynamic-datatemplate-with-valueconverter]]…XamlReader.Parseを使う。
**シングルクリックで編集する [#kc0801ee]
***基本 [#ua6f39ad]
-[[Windows Presentation Foundation (WPF) - Single-Click Editing:http://wpf.codeplex.com/wikipage?title=Single-Click%20Editing]]にある方法で可能。
-DataGridCellにPreviewMouseLeftButtonDownをセットし、事前にセルにフォーカスをあわせ選択状態にすることでシングルクリックで編集可能になる。
-Application全体でDataGridに共通のスタイルを設定している場合、EventSetterで設定するとスタイルが適用されなくなってしまうので、ResourceDictionaryを使って設定する(下記参照)
***すべてのDataGridでシングルクリック編集を可能にする [#l182e8fd]
-[[c# - Single click edit in WPF DataGrid - Stack Overflow:http://stackoverflow.com/questions/3426765/single-click-edit-in-wpf-datagrid]]に記述あり。
-ResourceDictionaryを使う。新規→リソースディクショナリでDataGrydStyles.xamlを追加。
#pre{{
<ResourceDictionary
x:Class="ControlDemo.DataGridDemo.SingleClick.DataGridStyles"
x:ClassModifier="public"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="DataGrid">
<!-- Your DataGrid style definition goes here -->
<Setter Property="CellStyle">
<Setter.Value>
<Style TargetType="DataGridCell">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown"/>
</Style>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
}}
-DataGridStyles.xaml.csを追加し、ここにDataGridCell_PreviewMouseLeftButtonDownを追加する。
#pre{{
namespace ControlDemo.DataGridDemo.SingleClick
{
partial class DataGridStyles : ResourceDictionary
{
//
// SINGLE CLICK EDITING
//
private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DataGridCell cell = sender as DataGridCell;
if (cell != null && !cell.IsEditing && !cell.IsReadOnly)
{
if (!cell.IsFocused)
{
cell.Focus();
}
DataGrid dataGrid = FindVisualParent<DataGrid>(cell);
if (dataGrid != null)
{
if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow)
{
if (!cell.IsSelected)
cell.IsSelected = true;
}
else
{
DataGridRow row = FindVisualParent<DataGridRow>(cell);
if (row != null && !row.IsSelected)
{
row.IsSelected = true;
}
}
}
}
}
static T FindVisualParent<T>(UIElement element) where T : UIElement
{
UIElement parent = element;
while (parent != null)
{
T correctlyTyped = parent as T;
if (correctlyTyped != null)
{
return correctlyTyped;
}
parent = VisualTreeHelper.GetParent(parent) as UIElement;
}
return null;
}
}
}
}}
***DataGridTemplateColumnで内部のコントロールにフォーカスするようにする [#n36c88d9]
-DataGridTextColumnなどでは上記シングルクリック対応により一発でセルが編集可能になるが、DataGridTemplateColumnでは編集状態になるものの、内部のコントロールにフォーカスしないため、もう一回クリックする必要がでてくる。
-これを解消するには、DataGridTemplateColumnを継承したクラスを使う。
#pre{{
public class CustomDataGridTemplateColumn :DataGridTemplateColumn
{
protected override object PrepareCellForEdit(FrameworkElement editingElement,
RoutedEventArgs editingEventArgs)
{
editingElement.MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
return base.PrepareCellForEdit(editingElement, editingEventArgs);
}
}
}}
-xamlでDataGridTemplateColumn→CustomDataGridTemplateColumnに変更する。
**行をDrag and Dropで入れ替える。セルベースで選択する [#m3db9f50]
-[[Common DataGrid Add-Ons:http://code.msdn.microsoft.com/Common-DataGrid-Add-Ons-4f64fcee]]ここにサンプルあり。
**三つの状態をもったソート [#o9daccd7]
-[[WPF DataGrid: Tri-state Sorting sample - Vincent Sibal's Blog - Site Home - MSDN Blogs:http://blogs.msdn.com/b/vinsibal/archive/2008/08/29/wpf-datagrid-tri-state-sorting-sample.aspx]]