Hello,
We have implemented custom insert logic in our grids via the context menu.
However, when there are no items in the grid, the user can't use the context menu so we show the "Click here to insert new row".
Once there's an item in the collection, the "Click here to insert new row" is no longer shown.
We are having problems pinpointing the best location at which to handle this.
We check if the grid doesn't have any items and if the "CanUserInsertRows" property is set to true.
If these conditions both are true, we show the insert new row area, otherwise we hide it:
I tried adding a value changed callback to the "CanUserInsertRows" dependency property, but it doesn't seem to trigger the callback when I do a Notify property changed on the property which is bound to the "CanUserInsertRows" property of the grid:
We used to call the "HandleNewRowPosition" method at several places (eg "OnItemsChanged", "OnLoaded", ...) but we noticed that it got called way to many times when we only expected it once, maybe twice.
Anyone have an idea on how to do this properly?
We have implemented custom insert logic in our grids via the context menu.
However, when there are no items in the grid, the user can't use the context menu so we show the "Click here to insert new row".
Once there's an item in the collection, the "Click here to insert new row" is no longer shown.
We are having problems pinpointing the best location at which to handle this.
We check if the grid doesn't have any items and if the "CanUserInsertRows" property is set to true.
If these conditions both are true, we show the insert new row area, otherwise we hide it:
public
void
HandleNewRowPosition()
{
var showNewRow = !GridTreeListHasItems() && Grid.CanUserInsertRows;
Grid.NewRowPosition = GetInsertNewRowPosition(showNewRow);
}
private
bool
GridTreeListHasItems()
{
var anyValue =
false
;
var x = Grid.ItemsSource
as
IEnumerable;
if
(x !=
null
)
{
anyValue = x.GetEnumerator().MoveNext();
}
return
anyValue;
}
private
GridViewNewRowPosition GetInsertNewRowPosition(
bool
show)
{
var converter =
new
BooleanToGridViewNewRowPositionConverter();
return
(GridViewNewRowPosition)converter.Convert(show,
typeof
(GridViewNewRowPosition),
null
, CultureInfo.CurrentUICulture);
}
/// <summary>
/// Converts a boolean to the position of new rows. True corresponds with the top.
/// </summary>
[ValueConversion(
typeof
(
bool
),
typeof
(GridViewNewRowPosition))]
public
class
BooleanToGridViewNewRowPositionConverter : IValueConverter
{
public
object
Convert(
object
value, Type targetType,
object
parameter, System.Globalization.CultureInfo culture)
{
return
(
bool
) value ? GridViewNewRowPosition.Top : GridViewNewRowPosition.None;
}
public
object
ConvertBack(
object
value, Type targetType,
object
parameter, System.Globalization.CultureInfo culture)
{
return
(GridViewNewRowPosition) value == GridViewNewRowPosition.Top;
}
}
I tried adding a value changed callback to the "CanUserInsertRows" dependency property, but it doesn't seem to trigger the callback when I do a Notify property changed on the property which is bound to the "CanUserInsertRows" property of the grid:
_canUserInserRowsPropertyDescriptor = DependencyPropertyDescriptor.FromProperty(GridViewDataControl.CanUserInsertRowsProperty,
typeof
(GridViewDataControl));
if
(_canUserInserRowsPropertyDescriptor !=
null
)
{
_canUserInserRowsPropertyDescriptor.AddValueChanged(
this
.AssociatedObject, CanUserInserRowsChanged_Handler);
}
private
void
CanUserInserRowsChanged_Handler(
object
sender, EventArgs e)
{
HandleNewRowPosition();
}
We used to call the "HandleNewRowPosition" method at several places (eg "OnItemsChanged", "OnLoaded", ...) but we noticed that it got called way to many times when we only expected it once, maybe twice.
Anyone have an idea on how to do this properly?