I'm having trouble with column widths on a datagrid. They always truncate part of a textblock in the cells. I have a converter which sets the minWidth of the textblock to the length of the string to be displayed but the column insists on truncating it.
Here is the code used to generate the DataGridTemplateColumn:
//create template for EDITING text parameters removed for brevity
//create template for SHOWING text parameters
DataTemplate dt2 = new DataTemplate();
FrameworkElementFactory tblk = new FrameworkElementFactory(typeof(TextBlock));
tblk.SetBinding(TextBlock.DataContextProperty, new Binding("doorparameters[" + pid.ToString() + "]"));
tblk.SetResourceReference(TextBlock.StyleProperty, "ParameterStringTextBlockStyle");
tblk.AddHandler(TextBlock.MouseRightButtonUpEvent, new MouseButtonEventHandler(this.StringParameter_MouseRightButtonUp));
FrameworkElementFactory b = new FrameworkElementFactory(typeof(Border));
b.SetResourceReference(Border.StyleProperty, "ParameterStringBorderStyle");
b.AppendChild(tblk);
dt2.VisualTree = b;
DataGridTemplateColumn dgtc = new DataGridTemplateColumn();
dgtc.HeaderStyle = (Style)_myDataGrid.FindResource("ParameterHeaderStyle");
dgtc.Width = DataGridLength.Auto; //SizeToCells is actually worse
dgtc.Header = dp.Name;
dgtc.CellEditingTemplate = dt;
dgtc.CellTemplate = dt2;
_myDataGrid.Columns.Add(dgtc);
And the following are the styles in xaml:
<Style x:Key="ParameterStringTextBlockStyle" TargetType="{x:Type TextBlock}">
<Setter Property="FontFamily" Value="{Binding Source={x:Static properties:Settings.Default}, Path=ScheduleFont.FontFamily.Name}" />
<Setter Property="FontSize" Value="{Binding Source={x:Static properties:Settings.Default}, Path=ScheduleFont, Converter={StaticResource sizeConverter}}" />
<Setter Property="MinHeight" Value="12" />
<Setter Property="Padding" Value="2,0,0,0"/>
<Setter Property="TextAlignment" Value="Left" />
<Setter Property="TextWrapping" Value="NoWrap"/>
<Setter Property="Tag" Value="{Binding Id}" />
<Setter Property="Text" Value="{Binding Value}" />
<Setter Property="Width" Value="Auto"/>
<Setter Property="MinWidth">
<Setter.Value>
<MultiBinding Converter="{StaticResource textwidthconverter}">
<Binding Path="Value" />
<Binding RelativeSource="{RelativeSource Mode=Self}"/>
</MultiBinding>
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsChecked}" Value="True">
<Setter Property="Background" Value="{StaticResource IsCheckedBrush}" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=IsReadOnly}" Value="True">
<Setter Property="Background" Value="{StaticResource ReadOnlyBrush}" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=HasChanged}" Value="True">
<Setter Property="Background" Value="{StaticResource HasChangedVBrush}" />
</DataTrigger>
</Style.Triggers>
</Style>
<Style x:Key="ParameterStringBorderStyle" TargetType="{x:Type Border}">
<Setter Property="BorderThickness" Value="2" />
<Setter Property="BorderBrush" Value="Black" />
<Setter Property="Width" Value="Auto" />
<Setter Property="MinHeight" Value="12" />
</Style>
I can't see what is causing the column width to truncate but I have noticed it only happens when the horizontal scroll bar is present. Any ideas?
Finally figured it out had to set all the DataGridTemplateColumn width = DataGridLength.SizeToCells. Some where set to "auto" which seems to mess up the grid.