2
我創建了自己的DataGrid控件,它繼承自DataGrid
。我聲明瞭一個我想在列級使用的Dependency Property,因此在PreviewKeyDown事件中,我檢查該值並決定是否需要處理當前單元格。幫助註冊依賴屬性
public class MyDataGrid : DataGrid
{
public static DependencyProperty HandleKeyPressEventProperty =
DependencyProperty.RegisterAttached(
"HandleKeyPressEvent",
typeof(bool),
typeof(MyDataGrid),
new FrameworkPropertyMetadata(true));
public bool HandleKeyPressEvent
{
get { return (bool)GetValue(HandleKeyPressEventProperty); }
set { SetValue(HandleKeyPressEventProperty, value); }
}
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
if (HandleKeyPressEvent)
{
HandleKeyPress(e);
}
else
{
base.OnPreviewKeyDown(e);
}
}
}
我的XAML看起來像這樣:
<MyDataGrid x:Name="myDataGrid">
<DataGridTextColumn MyDataGrid.HandleKeyPressEvent = "True" />
<DataGridTemplateColumn MyDataGrid.HandleKeyPressEvent = "False"/>
</MyDataGrid>
,但我有一個真正的問題有在列級可用此依賴項屬性。我試圖做的就像Grid.Column
。有人可以幫助我嗎?
謝謝Meleak。完美的作品 – Jing 2011-03-16 21:03:39