c# - 网格边框/单元格之间的间隙

标签 c# wpf xaml grid cells

我创建了一个 ControlTemplate,其中包含一个有两行的 Grid

遗憾的是,单元格之间似乎只有一个像素间隙。

如何消除间隙?

这是显示差距的屏幕截图:

Gap between cells

...这是 XAML:

<Window x:Class="MAQButtonTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="695" Width="996">        
    <Window.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="OverridesDefaultStyle" Value="True"/>
        </Style>
        <ControlTemplate TargetType="Button" x:Key="ButtonTemplate">
            <Grid Width="444" Margin="0" ShowGridLines="False">
                <Grid.RowDefinitions>
                    <RowDefinition Height="51" />
                    <RowDefinition Height="36" />
                </Grid.RowDefinitions>
                <Grid Grid.Row="0" Background="#286c97">
                    <TextBlock>This is the first piece of text</TextBlock>
                </Grid>
                <Grid Grid.Row="1" Background="#5898c0">
                    <ContentPresenter Grid.Row="0" />
                </Grid>
            </Grid>
        </ControlTemplate>
    </Window.Resources>
    <Grid>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="300" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid Grid.Column="0" Background="#e9f1f6"></Grid>
        <Grid Grid.Column="1" Background="#d2e3ed">
            <StackPanel>
                <TextBlock FontFamily="Segoe UI" FontSize="22" FontWeight="Medium" Margin="52,58,0,0" Foreground="#0c3d5d">Your Quizzes <TextBlock FontFamily="Segoe UI" FontSize="18" FontWeight="Medium" Foreground="#0c3d5d">(7)</TextBlock></TextBlock>
                <Grid Margin="0,20,0,0">
                    <Button Width="444" Background="{x:Null}" BorderThickness="0" Template="{StaticResource ButtonTemplate}" Click="DoSomething" BorderBrush="#032135">
                        <TextBlock Margin="6,2.8,0,0" FontFamily="Segoe UI" FontSize="19" Foreground="#032135" FontWeight="Medium">This is a second piece of text</TextBlock>
                    </Button>
                </Grid>
            </StackPanel>
        </Grid>

    </Grid>
</Window>

最佳答案

只需在模板网格中添加SnapsToDevicePixels="True"

<ControlTemplate TargetType="Button" x:Key="ButtonTemplate">
                <Grid Width="444" Margin="0" ShowGridLines="False" SnapsToDevicePixels="True">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="51" />
                        <RowDefinition Height="36" />
                    </Grid.RowDefinitions>
                    <Grid Grid.Row="0" Background="#286c97">
                        <TextBlock>This is the first piece of text</TextBlock>
                    </Grid>
                    <Grid Grid.Row="1" Background="#5898c0">
                        <ContentPresenter Grid.Row="0" />
                    </Grid>
                </Grid>
            </ControlTemplate>

关于c# - 网格边框/单元格之间的间隙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10718985/

相关文章:

c# - Azure Blob 存储生成无效的共享访问签名

c# - 将异步/等待与 Dispatcher.BeginInvoke() 结合使用

wpf - 如何在 XAML 中找到控件的默认值?

wpf - 在 WPF/XAML 中创建 Alt 键快捷方式

c# - WPF Application是否需要继承System.Windows.Application基类?

c# - 在不执行脚本的 nUnit 上运行 Selenium 测试

c# - 如何检查页面是否作为提交表单或其他方式的结果显示

c# - 适用于 iphone 应用程序的 MonoTouch & C# VS Objective C

c# - WPF - 无法将 ContentPresenter 中控件的属性绑定(bind)到父控件

wpf - 适用于 XAML 的 Windows Metro 中的 DrawingBrush 在哪里