如何绑定到Xceed DataGridControl列索引

时间:2017-02-10 11:20:43

标签: wpf xceed

在过去的两天里,我一直在尝试根据列的ACTUAL(不可见)索引为列的标题创建数据模板。任何人都可以请你告诉我如何正确地做到这一点?

<!--http://stackoverflow.com/questions/13693619/change-the-color-of-a-grid-header-using-xceed-datagrid-in-wpf-->
<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();
    }
}

2 个答案:

答案 0 :(得分:2)

ColumnManagerCell派生自Cell,其ParentColumn属性,父列具有VisiblePosition属性。绑定到可见列索引(在移动列时会发生变化)。

一般而言:

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

对于ControlTemplate

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

我使用xd作为xmlns:xd="http://schemas.xceed.com/wpf/xaml/datagrid"

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

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

修改

这是一个工作示例,源自您的代码和一些静态测试数据。请解释您需要的其他内容或您的情况有何不同之处。在该示例中,列标题中静态添加的数字等于动态添加的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;
}

答案 1 :(得分:0)

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