Reputation: 2309
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; }
set
{
_model = value;
OnPropertyChanged();
}
}
Upvotes: 0
Views: 1554
Reputation: 2309
In the end I wasn't able to find a solution so I just used the built in DataGrid. The problem is that I cannot retrieve the actual indices in any way.
Upvotes: 0
Reputation: 13438
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>
Edit
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;
}
Upvotes: 2