在调整包含它的列时,动态设置Textblock大小

时间:2012-07-12 16:13:06

标签: c# wpf xceed-datagrid

我有一个xceed网格(不确定这是否重要),其中一列有一个具有TextBlock的CellContentTemplate。而不是文本块宽度自动设置为文本的大小,我希望它是列的大小。我允许调整大小,所以这会使事情复杂化。有没有办法在列调整后动态更改TextBlock宽度,在代码后面?

编辑: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>

1 个答案:

答案 0 :(得分:1)

嗨,这是因为你将TextBlock封装在StackPanel中。一个简单的解决方法是使用两个Run来绑定两个属性,如

,而不是两个TextBlock
<DataGridTemplateColumn Header="Name" Width="*" >
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                            <TextBlock Background="Green">
                                <Run Text="{Binding Name}"/>
                                <Run Text="{Binding Rollno}"/>
                            </TextBlock>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

现在textblock将以列的宽度扩展。我希望这会有所帮助。