StaWho
StaWho

Reputation: 2488

Binding to WPF DataGrid cell value from within template

I have a collection of custom data objects that I display in DataGrid. The columns are created dynamically on runtime. Some of the columns are TemplateColumns which display the value as a progressbar with respective text in the TextBlock; the brogressbar itself is defined from Style:

private string CreateProgressBarColumnTemplate(string fieldName)
{
    StringBuilder CellTemp = new StringBuilder();
    CellTemp.Append("<DataTemplate ");
    CellTemp.Append("xmlns='http://schemas.microsoft.com/winfx/");
    CellTemp.Append("2006/xaml/presentation' ");
    CellTemp.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>");
    CellTemp.Append(String.Format("<ProgressBar Margin=\"0,1,0,0\" MinWidth=\"100\" MaxWidth=\"Infinity\" MaxHeight=\"Infinity\" Width=\"Auto\" Height=\"Auto\" HorizontalAlignment=\"Stretch\"  VerticalAlignment=\"Stretch\" Value=\"{{Binding {0}}}\" Style=\"{{StaticResource ProgressBarStyle}}\"/>", fieldName));
    CellTemp.Append("</DataTemplate>");
    return CellTemp.ToString();
}

private DataGridTemplateColumn CreateProgressBarTemplateColumn(string fieldName, string columnHeader)
{
    DataGridTemplateColumn column = new DataGridTemplateColumn();
    column.CanUserSort = true;
    column.CanUserResize = false;
    column.Header = columnHeader;
    column.CellTemplate = (DataTemplate)XamlReader.Parse(CreateProgressBarColumnTemplate(fieldName)); //display template
    return column;
}
<Style x:Key="ProgressBarStyle" TargetType="ProgressBar">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ProgressBar">
                <Border BorderBrush="#BBC6C4" BorderThickness="1" CornerRadius="5" Padding="1">
                    <Grid x:Name="PART_Track" >
                        <Rectangle x:Name="PART_Indicator" HorizontalAlignment="Left" RadiusX="5" RadiusY="5">
                            <Rectangle.Fill>
                                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                    <GradientStop Color="#FF1D5666" Offset="1"/>
                                    <GradientStop Color="#FF09B6FF"/>
                                </LinearGradientBrush>
                            </Rectangle.Fill>
                        </Rectangle>
                        <TextBlock FontFamily="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=FontFamily}" 
                                   FontSize="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=FontSize}" 
                                   FontWeight="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=FontWeight}" 
                                   TextAlignment="Center" 
                                   Background="Transparent" 
                                   DataContext="{TemplateBinding Value}" 
                                   Foreground="{Binding Converter={StaticResource ValueToColor}}" x:Name="ProgressText" 
                                   Margin="0,-3,0,0" 
                                   Text="{Binding Converter={StaticResource DoubleToPercentage}}"
                                   />
                    </Grid>              
                </Border>       
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

As you can see, the TextBlock DataContext is set to Value property of progressbar and the TextBlock.Text is set from it.

The problem is that Value property can not be higher that MaxValue (Value seems to be set to MaxValue if I try assigning greater value), and my TextBlock.Text will always show 100% even if the actual cell value is greater than this.

The problem is, how do I bind TextBlock.Text property to actual cell value (the property on my custom data object, which is dynamic) and not to a progressbar?

Upvotes: 2

Views: 1941

Answers (1)

CodeNaked
CodeNaked

Reputation: 41393

You can use the ProgressBar.Tag property to proxy the value to the TextBlock in the ControlTemplate. But really, this should be a custom control (i.e. MyProgressBar) which exposes a new dependency property (i.e. Text).

To use the Tag, you'd add:

CellTemp.Append(String.Format("<ProgressBar ... Tag=\"{{Binding {0}}}\" ... />", fieldName));

The bind your TextBlock to it:

<TextBlock ... DataContext="{TemplateBinding Tag}" ... />

Upvotes: 1

Related Questions