0

I have an xceed grid (not sure if that is important) and one of the columns has a CellContentTemplate that has a TextBlock. Instead of the textblock width being automatically set to the size of the text, I want it to be the size of the column. I allow resizing, so this complicates things a bit. Is there a way to dynamically change the TextBlock width when the column gets resized, in code behind?

EDIT: CellContentTemplate

 <DataTemplate x:Key="CellTemplate.TaskName">
        <StackPanel Orientation="Horizontal">
            <TextBlock Tag="{Binding Path=DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}">
                   <TextBlock Text="{Binding Path=Name}" >
                       <ui:DragDropBinding.DragSource>
                           <Binding Mode="TwoWay" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type XceedProviders:DataGridControlWithSelectedItems}}" Path="BindableSelectedItems" PresentationTraceSources.TraceLevel="High"/>
                       </ui:DragDropBinding.DragSource>
                   </TextBlock>
               </TextBlock>
        </StackPanel>
    </DataTemplate>
g.t.w.d
  • 601
  • 1
  • 10
  • 30

1 Answers1

1

Hi this is because you enclosing TextBlock in StackPanel . One simple workaround is instead of two TextBlock use two Run to bind two properties like

<DataGridTemplateColumn Header="Name" Width="*" >
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                            <TextBlock Background="Green">
                                <Run Text="{Binding Name}"/>
                                <Run Text="{Binding Rollno}"/>
                            </TextBlock>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

Now textblock will expand with the width of column .I hope this will help.

yo chauhan
  • 12,079
  • 4
  • 39
  • 58
  • What in there actually causes it to expand with the column width? – g.t.w.d Jul 12 '12 at 18:04
  • by default when you have TextBlock/(any other content control) in DataGridTemplateColumn it covers entire width but if you will enclose it in stackpanel then it will have width only how much its text require – yo chauhan Jul 12 '12 at 18:13
  • I am using Xceed, so I won't be using DataGridTemplateColumn. I will try this though. Maybe xceed works the same way. – g.t.w.d Jul 12 '12 at 19:01