如何根据用户请求调用 IValueConverter? [XamDataGrid记录索引]

发布于 2024-10-27 20:19:50 字数 1934 浏览 5 评论 0原文

我想在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼眸 2024-11-03 20:19:50

当通过 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.

情泪▽动烟 2024-11-03 20:19:50

我在 Infragistics 支持的帮助下找到了解决方案。它结合了多种方法。所以...

编写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

}

注册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); }
        }

编写风格:

<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>

添加用于排序事件的事件处理程序、删除事件等,使用以下代码:

this.DummyValue++;

就这样。祝你好运 :)。

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 :).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文