WPF Gridsplitter不能与UseLayoutRounding一起使用

时间:2015-02-11 21:27:11

标签: wpf

如果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组件解决它仍然会很好。

1 个答案:

答案 0 :(得分:0)

有趣的问题。一个明显的解决方法是为至少一个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> 
相关问题