如何绑定到 Xceed DataGridControl 列索引

How to bind to Xceed DataGridControl column index

在过去的两天里,我一直在尝试根据列的 ACTUAL(不可见)索引为列的 ​​header 创建数据模板。谁能告诉我如何正确地做到这一点?

<!--
<ControlTemplate x:Key="HeaderTemplate" TargetType="{x:Type xcdg:ColumnManagerCell}">
    <DockPanel>
        <TextBlock DockPanel.Dock="Top" Text="{TemplateBinding Content}" x:Name="TextContainer"/>
        <TextBlock Visibility="{Binding Step, Converter={StaticResource Mapping}}" x:Name="WorkElement" DockPanel.Dock="Top" Foreground="Red" Width="100">
            <TextBlock.Text>
                <MultiBinding Converter="{StaticResource Conv}">
                    <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="ParentColumn.VisiblePosition"></Binding>
                    <Binding Path= "FileModel.Columns"></Binding>
                    <Binding ElementName="TextContainer" Path="Text"></Binding>
                    <Binding ElementName="WorkElement" Path="Text"></Binding>
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>
    </DockPanel>
</ControlTemplate>
<Style TargetType="{x:Type xcdg:ColumnManagerRow}">
    <Setter Property="Background" Value="AliceBlue"/>
    <Setter Property="BorderBrush" Value="Black"/>
    <Setter Property="AllowColumnReorder" Value="False"/>
</Style>

<Style TargetType="{x:Type xcdg:ColumnManagerCell}">
    <Setter Property="Template" Value="{StaticResource HeaderTemplate}"/>
</Style>

转换器:

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
    var context = values[1] as IEnumerable<MatrixImportColumn>;
    if ( values[1] != null && values[0] != null 
        && values[0] != DependencyProperty.UnsetValue
        && values[1] != DependencyProperty.UnsetValue )
    {
        var itemContext = (int)values[0];
        var original = values[2] as string;
        if (context != null)
        {
            var dp = context.FirstOrDefault(x => x.ColumnIndex == itemContext);
            return string.Format("{0} -> {1}", original, dp.MappedInto);
        }

    return string.Format("{0} -> [Unmapped]", original);
    }

    var val = values[3] as string;
    if (val != string.Empty)
        return val;
    else return "";
}

我的模型是:

public DataTable Model
{
    get { return _model; }
    set
    {
        _model = value;
        OnPropertyChanged();
    }
}

ColumnManagerCell 派生自 Cell,它有一个 ParentColumn 属性 并且 parent 列有一个 VisiblePosition 属性.绑定到可见列索引(移动列时会发生变化)。

一般:

<TextBlock Text="{Binding Path=ParentColumn.VisiblePosition,RelativeSource={RelativeSource AncestorType={x:Type xd:Cell}}}"/>

为了你的ControlTemplate

<TextBlock Text="{Binding Path=ParentColumn.VisiblePosition,RelativeSource={RelativeSource TemplatedParent}}"/>

我用xdxmlns:xd="http://schemas.xceed.com/wpf/xaml/datagrid"

为了获得一致的可见位置,您可以禁用 ColumnManagerRow 样式中的列重新排序。

<Style TargetType="xd:ColumnManagerRow">
    <Setter Property="AllowColumnReorder" Value="False"/>
</Style>

编辑

这是一个工作示例,源自您的代码和一些静态测试数据。请解释您还需要什么或您的情况有何不同。在示例中,header列中静态添加的数字等于动态添加的VisiblePosition数字。

<Grid x:Name="grid1">
    <xd:DataGridControl ItemsSource="{Binding Data}">
        <xd:DataGridControl.Resources>
            <ControlTemplate x:Key="HeaderTemplate" TargetType="{x:Type xd:ColumnManagerCell}">
                <DockPanel>
                    <TextBlock DockPanel.Dock="Top" Text="{TemplateBinding Content}" x:Name="TextContainer"/>
                    <TextBlock Text="{Binding Path=ParentColumn.VisiblePosition,RelativeSource={RelativeSource TemplatedParent}}" Foreground="Red"/>
                </DockPanel>
            </ControlTemplate>
            <Style TargetType="{x:Type xd:ColumnManagerRow}">
                <Setter Property="Background" Value="AliceBlue"/>
                <Setter Property="BorderBrush" Value="Black"/>
                <Setter Property="AllowColumnReorder" Value="False"/>
            </Style>
            <Style TargetType="{x:Type xd:ColumnManagerCell}">
                <Setter Property="Template" Value="{StaticResource HeaderTemplate}"/>
            </Style>
        </xd:DataGridControl.Resources>
    </xd:DataGridControl>
</Grid>

数据创建的隐藏代码

public DataTable Data { get; set; }
private void Window_Loaded(object sender, RoutedEventArgs e)
{
    Data = new DataTable("My Data Table");
    for (int i = 0; i < 100; i++)
    {
        Data.Columns.Add("Column " + i + " Head");
    }
    for (int i = 0; i < 10; i++)
    {
        var row = Data.NewRow();
        for (int j = 0; j < 100; j++)
        {
            row.SetField(j, string.Format("{0},{1}", i, j));
        }
        Data.Rows.Add(row);
    }
    grid1.DataContext = this;
}

最后我找不到解决方案,所以我只使用了内置的 DataGrid。问题是我无法以任何方式检索实际索引。