Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

For the last two days I've been trying create a data template for the header of the column based on the ACTUAL( not visible ) index of the column. Can anyone please enlighten me how to do that correctly?

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

Converter:

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

My model is :

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

The ColumnManagerCell derives from Cell which has a ParentColumn property and the parent column has a VisiblePosition property. Bind to this for the visible column index (will change when moving columns around).

Generally:

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

For your ControlTemplate:

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

I use xd as xmlns:xd="http://schemas.xceed.com/wpf/xaml/datagrid"

In order to have a consistent visible position, you can disable column reordering in the ColumnManagerRow style.

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

Here is a working example, derived from your code with some static test data. Please explain what else you need or what is different in your case. In the example, the statically added number in the column header equals the dynamically added VisiblePosition number.

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

Code behind for data creation

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;
                Just to make sure, visible position isn't affected in some way by the virtualization right?
– Christo S. Christov
                Feb 10, 2017 at 12:07
                @Hristo Then explain which kind of index you are talking about. If it's something from the model, then include an index property for the model.
– grek40
                Feb 10, 2017 at 12:21
                the problem is that there is no way for me to access the model of the column as I'm feeding in a DataTable object as the ItemsSource
– Christo S. Christov
                Feb 10, 2017 at 12:22
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.