I want to add record index like MS Excel in my XamDataGrid control. I use for this trick with IValueConverter. I define some template for displaying record indexes width this XAML-code:
<local:RowNumberConverter x:Key="rowNumberConverter" />
<Style TargetType="{x:Type igDP:RecordSelector}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type igDP:RecordSelector}">
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticRes开发者_C百科ource rowNumberConverter}">
<Binding />
<Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type igDP:XamDataGrid}}"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
RowNumberConverter defined as:
class RowNumberConverter : IMultiValueConverter {
#region IMultiValueConverter Members
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
//get the grid and the item
Object item = values[0];
XamDataGrid grid = values[1] as XamDataGrid;
int index = grid.RecordManager.Unsorted.IndexOf(((DataRecord)item));
return index.ToString();
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) {
throw new NotImplementedException();
}
#endregion
}
This works perfectly when I add records in my XamDataGrid instance, but when I sort data by any field in my datagrid, record indexes also sorts (value converter does not calling when I press sort button). Maybe I can call it manually?
Thanks for any advices and sorry for my bad english.
Value conversion happens when the properties are accessed (get/set) via DataBinding. You're sorting is not triggering this and I don't suspect you want it to since your sort probably does not work on the original collection and you'd probably get the items with the wrong row index on them again. I don't know of anyway, other than explicitly triggering a binding refresh, of getting the ValueConverter implementation to work.
Check this MSDN article for information on explicitly calling the UpdateSource method. I don't know if this will solve your problem unless you sort on the original object collection with LINQ.
I found solution with help of Infragistics support. It's combines several methods. So...
Write MultiValueConverter:
public class RowNumberConverter : IMultiValueConverter {
#region IMultiValueConverter Members
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (values != null && values[0] != null)
{
Record r = values[0] as Record;
return (r.VisibleIndex + 1).ToString();
}
else
return null;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
Register DepencyProperty:
public static readonly DependencyProperty DummyValueProperty =
DependencyProperty.Register("DummyValue", typeof(int), typeof(XamRibbonWindow), new UIPropertyMetadata(0));
public int DummyValue
{
get { return (int)GetValue(DummyValueProperty); }
set { SetValue(DummyValueProperty, value); }
}
Write style:
<Style TargetType="{x:Type igDP:RecordSelector}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type igDP:RecordSelector}">
<Border BorderBrush="#FFD4D4D4" BorderThickness="0,1.5,1.5,0" Background="#FFF1F1F1">
<TextBlock HorizontalAlignment="Center" Margin="0" TextWrapping="Wrap" VerticalAlignment="Center" FontSize="{Binding Path=FontSize, RelativeSource={RelativeSource AncestorType={x:Type igDP:XamDataGrid}}}">
<TextBlock.Text>
<MultiBinding Converter="{StaticResource ResourceKey=rowNumberConverter}">
<Binding />
<Binding Path="DummyValue" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type Window}}"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="MinWidth" Value="50"/>
</Style>
Add event handlers for sorting event, delete event etc. with this code:
this.DummyValue++;
Thats all. Good luck :).
精彩评论