none
当我修改datagrid的列宽时,stackpanel的textblock不会随之改变,该如何解决? RRS feed

  • 问题

  • textblock中设置了TextTrimming="CharacterEllipsis",预计拉近datagrid的列宽时textblock会显示...并在其右有个button,现在的代码运行的结果中,拉远时text block和button都没有靠右,拉近时也不会出现...,请问该如何实现?

    <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate >
                                <StackPanel  Orientation="Horizontal" >
                                    <TextBlock  Width="130" TextTrimming="CharacterEllipsis"  >
    </TextBlock>
                                    <Button  HorizontalAlignment="Right">...</Button>
                                </StackPanel>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>

    2020年11月13日 9:12

答案

  • 你好,

    你可以把你的TextBlock的Width设置成:Width="{Binding ElementName=MyCellTemplate}" ,我修改了你的代码,如下所示:

     <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel>
                                    <WrapPanel Name="MyCellTemplate" Width="Auto">
                                        <TextBlock Text="{Binding Number}" Width="{Binding ElementName=MyCellTemplate}" TextTrimming="CharacterEllipsis"  ></TextBlock>
                                        <Button  HorizontalAlignment="Right">...</Button>
                                    </WrapPanel>
                                </StackPanel>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>

    测试时,拉近时可以出现你想的效果。

    谢谢

    Daisy Tian


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2020年11月16日 5:26