While creating a custom control of nested grid i am not able to bind child grid. Can any body help me into it?
Below code is a custom control code
Code Below:
using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.Collections;
using System.Windows.Controls.Primitives;
using System.Data;
namespace NestedCustomControl
{
public class NestedGridControl : DataGrid
{
public override void BeginInit()
{
FrameworkElementFactory dtGrid = new FrameworkElementFactory(typeof(DataGrid));
dtGrid.SetValue(DataGrid.ItemsSourceProperty, new Binding("setChildBind"));
DataTemplate dttemp = new DataTemplate();
dttemp.VisualTree = dtGrid;
this.RowDetailsTemplate = dttemp;
base.BeginInit();
}
public BindingBase setChildBind
{
get
{
return (BindingBase)GetValue(SetChildBinding);
}
set
{
SetValue(SetChildBinding,value);
}
}
public static readonly DependencyProperty SetChildBinding =
DependencyProperty.Register("ChildBinding", typeof(BindingBase),
typeof(NestedGridControl), new FrameworkPropertyMetadata(OnChildGridSourceChange));
private static void OnChildGridSourceChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
}
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
DependencyObject dep = (DependencyObject)e.OriginalSource;
//navigate up the tree
while (dep != null && !(dep is DataGridCell) && !(dep is DataGridColumnHeader))
{
dep = VisualTreeHelper.GetParent(dep);
}
if (dep == null)
{
return;
}
DataGridCell dgc = dep as DataGridCell;
if (dgc != null)
{
//navigate further up the tree
while (dep != null && !(dep is DataGridRow))
{
dep = VisualTreeHelper.GetParent(dep);
}
DataGridRow row = dep as DataGridRow;
DataGrid dg = this as DataGrid;
//row.Focusable = true;
bool focused = row.Focus();
FocusNavigationDirection focusDirection = FocusNavigationDirection.Down;
FocusNavigationDirection focusDirectionUp = FocusNavigationDirection.Previous;
TraversalRequest request = new TraversalRequest(focusDirection);
TraversalRequest requestUp = new TraversalRequest(focusDirectionUp);
UIElement elementWithFocus = Keyboard.FocusedElement as UIElement;
if (e.Key == Key.Down)
{
if (!e.Handled)
{
if (row.Background == Brushes.Gray && row.DetailsTemplate!=null)
{
row.DetailsVisibility = System.Windows.Visibility.Visible;
}
elementWithFocus.MoveFocus(request);
e.Handled = true;
}
}
}
}
}
}