Hello,
I am using
this example to get a row number in my gridview:
http://www.telerik.com/forums/how-to-display-the-row-number
I altered the code from the example and added a dependency property to it, which I am binding to a property of an item from the itemssource collection of the gridview.
public
class
RowNumberPresenter : TextBlock
{
private
GridViewDataControl _parentControl;
public
GridViewDataControl ParentControl
{
get
{
return
this
._parentControl; }
private
set
{
if
(
this
._parentControl !=
null
)
this
._parentControl.Items.CollectionChanged -=
new
NotifyCollectionChangedEventHandler(Items_CollectionChanged);
this
._parentControl = value;
if
(
this
._parentControl !=
null
)
this
._parentControl.Items.CollectionChanged +=
new
NotifyCollectionChangedEventHandler(Items_CollectionChanged);
}
}
public
RowNumberPresenter(GridViewDataControl parentControl,
object
dataItem)
:
base
()
{
ParentControl = parentControl;
SetText(dataItem);
}
private
void
Items_CollectionChanged(
object
sender, NotifyCollectionChangedEventArgs e)
{
SetText(
this
.DataContext);
}
private
void
SetText(
object
dataItem)
{
if
(
this
.ParentControl.IsGrouping)
{
var group =
this
.ParentControl.FindGroupByItem(dataItem);
if
(group !=
null
)
{
var items = group.Items
as
ReadOnlyObservableCollection<
object
>;
if
(items !=
null
)
this
.Text = (items.IndexOf(dataItem) + 1).ToString();
}
}
if
(
string
.IsNullOrWhiteSpace(
this
.Text))
this
.Text = (
this
.ParentControl.Items.IndexOf(dataItem) + 1).ToString();
}
}
public
class
RowNumberGridViewColum : GridViewColumn
{
private
RowNumberPresenter _rowNumberPresenter;
public
override
FrameworkElement CreateCellElement(GridViewCell cell,
object
dataItem)
{
_rowNumberPresenter = cell.Content
as
RowNumberPresenter;
if
(_rowNumberPresenter ==
null
)
_rowNumberPresenter =
new
RowNumberPresenter(
this
.DataControl, dataItem);
CurrentIndex = Int32.Parse(_rowNumberPresenter.Text);
return
_rowNumberPresenter;
}
public
int
CurrentIndex
{
get
{
return
(
int
)GetValue(CurrentIndexProperty); }
set
{ SetValue(CurrentIndexProperty, value); }
}
public
static
readonly
DependencyProperty CurrentIndexProperty =
DependencyProperty.Register(
"CurrentIndex"
,
typeof
(
int
),
typeof
(RowNumberGridViewColum));
}
<
cControlsTelerik:RowNumberGridViewColum
IsReadOnly
=
"True"
TabStopMode
=
"Skip"
TextAlignment
=
"Center"
CurrentIndex
=
"{Binding Path=OrderRowData.SequenceNumber}"
>
<
cControlsTelerik:RowNumberGridViewColum.Header
>
<
TextBlock
Text
=
"No.:"
/>
</
cControlsTelerik:RowNumberGridViewColum.Header
>
</
cControlsTelerik:RowNumberGridViewColum
>
The binding
is not working and I am I am a little stuck how to bind the rownumber of the
row to my dataobject (a OrderRow object).
I receive
the error that OrderRowData is not a property of OrderViewModel, OrderViewModel is my VM for the whole view?
The
Itemssource of the gridview is set to a Rows property on my OrderViewModel,
this Rows collection contains all the OrderRowData instances visible in the
grid.
Any help would be appreciated.
Regards,
Marcel