Hi alexchou1984,
Here's a small sample following what you say. I use the LayoutUpdated callback to get a specific DataCell from the DatagridControl. This works !
Here's a small sample following what you say. I use the LayoutUpdated callback to get a specific DataCell from the DatagridControl. This works !
<StackPanel>
<xcdg:DataGridControl x:Name="_datagrid" />
<Button Content="Refresh"
Click="Button_Click" />
</StackPanel>
public partial class MainWindow : Window
{
List<MyData> _theList = new List<MyData>()
{
new MyData() { Age = 22, FirstName = "Tom", LastName = "Tompson"},
new MyData() { Age = 45, FirstName = "Mark", LastName = "Jones"},
new MyData() { Age = 63, FirstName = "Karen", LastName = "Bartley"},
new MyData() { Age = 55, FirstName = "Julia", LastName = "Lapson"},
new MyData() { Age = 31, FirstName = "Mike", LastName = "Mouse"},
};
MyData _newItem = new MyData() { Age = 25, FirstName = "Angelina", LastName = "Sawyer"};
public MainWindow()
{
InitializeComponent();
_datagrid.ItemsSource = _theList;
}
private void Button_Click( object sender, RoutedEventArgs e )
{
_theList.Add( _newItem );
_datagrid.Items.Refresh();
DependencyObject dependencyObject = _datagrid.GetContainerFromItem( _newItem );
if( dependencyObject == null )
{
System.Diagnostics.Debug.WriteLine( "Container is null at this point" );
_datagrid.LayoutUpdated += this.Datagrid_LayoutUpdated;
}
}
private void Datagrid_LayoutUpdated( object sender, EventArgs e )
{
DataRow dataRow = _datagrid.GetContainerFromItem( _newItem ) as DataRow;
if( dataRow != null )
{
DataCell dataCell = dataRow.Cells[ "Age" ] as DataCell;
if( dataCell != null )
{
System.Diagnostics.Debug.WriteLine( "Background of DataCell is : " + dataCell.Background.ToString() );
}
}
_datagrid.LayoutUpdated -= this.Datagrid_LayoutUpdated;
}
}
public class MyData
{
public string FirstName
{
get;
set;
}
public string LastName
{
get;
set;
}
public int Age
{
get;
set;
}
}