如何根据用户请求调用 IValueConverter? [XamDataGrid记录索引]
我想在我的 XamDataGrid 控件中添加像 MS Excel 一样的记录索引。我将这个技巧与 IValueConverter 一起使用。我定义了一些用于显示记录索引宽度的模板,该 XAML 代码:
<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="{StaticResource rowNumberConverter}">
<Binding />
<Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type igDP:XamDataGrid}}"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
RowNumberConverter 定义为:
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
}
当我在 XamDataGrid 实例中添加记录时,这非常有效,但是当我按数据网格中的任何字段对数据进行排序时,记录索引也会排序(值转换器确实如此)当我按下排序按钮时不打电话)。也许我可以手动调用它?
感谢您的任何建议,并对我的英语不好表示歉意。
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="{StaticResource 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当通过 DataBinding 访问(获取/设置)属性时,会发生值转换。您的排序不会触发此操作,我不怀疑您想要它,因为您的排序可能不适用于原始集合,并且您可能会再次获得行索引错误的项目。无论如何,除了显式触发绑定刷新之外,我不知道如何让 ValueConverter 实现正常工作。
请查看这篇 MSDN 文章,了解有关以下内容的信息:显式调用 UpdateSource 方法。我不知道这是否能解决您的问题,除非您使用 LINQ 对原始对象集合进行排序。
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.
我在 Infragistics 支持的帮助下找到了解决方案。它结合了多种方法。所以...
编写MultiValueConverter:
public class RowNumberConverter : IMultiValueConverter {
}
注册DepencyProperty:
编写风格:
添加用于排序事件的事件处理程序、删除事件等,使用以下代码:
就这样。祝你好运 :)。
I found solution with help of Infragistics support. It's combines several methods. So...
Write MultiValueConverter:
public class RowNumberConverter : IMultiValueConverter {
}
Register DepencyProperty:
Write style:
Add event handlers for sorting event, delete event etc. with this code:
Thats all. Good luck :).