public class DataNotFoundBehavior:Behavior<SfDataGrid>
{
protected override void OnAttached()
{
this.AssociatedObject.Loaded += AssociatedObject_Loaded;
}
private void AssociatedObject_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
if (this.AssociatedObject.View.Records.Count <= 0)
{
this.AssociatedObject.UnBoundRows.Add(new GridUnBoundRow() { Position = UnBoundRowsPosition.Top });
this.AssociatedObject.UnBoundRowCellStyle = Application.Current.MainWindow.Resources["style"] as Style;
this.AssociatedObject.QueryRowHeight += AssociatedObject_QueryRowHeight;
this.AssociatedObject.QueryUnBoundRow += AssociatedObject_QueryUnBoundRow;
this.AssociatedObject.QueryCoveredRange += AssociatedObject_QueryCoveredRange;
}
}
private void AssociatedObject_QueryCoveredRange(object sender, GridQueryCoveredRangeEventArgs e)
{
var unboundRow = this.AssociatedObject.GetUnBoundRow(e.RowColumnIndex.RowIndex);
if (unboundRow == null)
return;
else if (e.RowColumnIndex.ColumnIndex == 0)
{
e.Range = new CoveredCellInfo(e.RowColumnIndex.ColumnIndex, (e.OriginalSender as SfDataGrid).Columns.Count, e.RowColumnIndex.RowIndex, e.RowColumnIndex.RowIndex);
e.Handled = true;
}
}
private void AssociatedObject_QueryUnBoundRow(object sender, GridUnBoundRowEventsArgs e)
{
if (e.RowColumnIndex.ColumnIndex == 0)
{
e.Value = "Items not found";
e.Handled = true;
}
}
private void AssociatedObject_QueryRowHeight(object sender, QueryRowHeightEventArgs e)
{
if (AssociatedObject.IsUnBoundRow(e.RowIndex))
{
e.Height = 150;
e.Handled = true;
}
}
} |