Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding to WPF DataGrid cell value from within template
    primarykey
    data
    text
    <p>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:</p> <pre class="lang-cs prettyprint-override"><code>private string CreateProgressBarColumnTemplate(string fieldName) { StringBuilder CellTemp = new StringBuilder(); CellTemp.Append("&lt;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'&gt;"); CellTemp.Append(String.Format("&lt;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}}\"/&gt;", fieldName)); CellTemp.Append("&lt;/DataTemplate&gt;"); 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; } </code></pre> <pre class="lang-xml prettyprint-override"><code>&lt;Style x:Key="ProgressBarStyle" TargetType="ProgressBar"&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="ProgressBar"&gt; &lt;Border BorderBrush="#BBC6C4" BorderThickness="1" CornerRadius="5" Padding="1"&gt; &lt;Grid x:Name="PART_Track" &gt; &lt;Rectangle x:Name="PART_Indicator" HorizontalAlignment="Left" RadiusX="5" RadiusY="5"&gt; &lt;Rectangle.Fill&gt; &lt;LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"&gt; &lt;GradientStop Color="#FF1D5666" Offset="1"/&gt; &lt;GradientStop Color="#FF09B6FF"/&gt; &lt;/LinearGradientBrush&gt; &lt;/Rectangle.Fill&gt; &lt;/Rectangle&gt; &lt;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}}" /&gt; &lt;/Grid&gt; &lt;/Border&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; </code></pre> <p>As you can see, the TextBlock DataContext is set to Value property of progressbar and the TextBlock.Text is set from it.</p> <p>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.</p> <p>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? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload