【问题标题】:WPF Gridsplitter does not work with UseLayoutRoundingWPF Gridsplitter 不适用于 UseLayoutRounding
【发布时间】:2015-03-18 02:42:48
【问题描述】:

如果 wpf 应用程序已将窗口的 UseLayoutRounding 设置为 true,则 gridsplitter 将不再适用于某些窗口宽度(它将卡在其原始位置),如下例所示。

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="200" Width="401"
        UseLayoutRounding="True">

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <TextBlock Grid.Column="0">
            long string that does not fit within the textblock - long string that does not fit within the textblock
        </TextBlock>

        <GridSplitter Grid.Column="1" HorizontalAlignment="Center" Width="50" Background="LightBlue" />

        <TextBlock Grid.Column="2">
            long string that does not fit within the textblock - long string that does not fit within the textblock
        </TextBlock>
    </Grid>
</Window>

请注意,对于生成错误的示例,窗口宽度必须为 401,文本框中的文本必须比文本框长,并且 UseLayoutRounding 必须为 true。

任何知道如何避免这种情况或有任何解决方法的人?我不想将 UseLayoutRounding 设置为 false,因为它会在我的应用程序中导致呈现伪影。

编辑: 对于其他有同样问题的人,我发现这个用户制作的组件可以解决这个问题:http://blog.onedevjob.com/2011/12/11/fixing-wpf-gridsplitter/ 不过如果可以使用默认的 wpf 组件来解决它仍然会很好。

【问题讨论】:

    标签: wpf


    【解决方案1】:

    有趣的问题。一种明显的解决方法是为至少一个 TextBlock 元素设置 TextWrapping="WrapWithOverflow" 但我确定这不是您的意图。

    另一种解决方法是不要给拆分器它自己的网格列,而是将其放在第二列,最后一列。似乎可以解决问题:

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
    
        <TextBlock Grid.Column="0">
            long string that does not fit within the textblock - long string that does not fit within the textblock
        </TextBlock>
    
        <GridSplitter Grid.Column="1" Margin="-25,0,0,0" HorizontalAlignment="Left" Width="50" Background="LightBlue" />
    
        <TextBlock Grid.Column="1" Margin="25,0,0,0">
            long string that does not fit within the textblock - long string that does not fit within the textblock
        </TextBlock>
    </Grid> 
    

    【讨论】:

    • 感谢您的回复,但它并没有真正起作用,它只是改变了拆分器卡住的窗口宽度。上面代码的窗口宽度为 403 仍然会产生错误。
    猜你喜欢
    • 1970-01-01
    • 2010-12-23
    • 1970-01-01
    • 2015-07-30
    • 2010-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多