2009-10-19 116 views
2

我使用WPF网格对齐对象,跑进一个相当突出的问题,关于列像素排列。我试图删除尽可能多的变量可能的,并设法显示问题在此代码:WPF网格象素对齐问题

<Window x:Class="Test.Window1" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="Window1" Height="300" Width="300"> 
<Grid> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="100.5" /> 
     <ColumnDefinition Width="199.5" /> 
    </Grid.ColumnDefinitions> 
    <Border Background="Red"> 
     <Grid.Column>1</Grid.Column> 
    </Border> 
    <Border Background="Red"> 
    </Border> 
</Grid> 
</Window> 

如果您运行的样本,可以很容易地看到,有一个在两列之间的边界问题。它造成的,我相信,因为WPF简单字母混合的背景一列,另其结果,即使在概念上都列在同一Z,所以像素应该是他们的权重和总和的混合背景。

我明白这是一个棘手的问题,自然我不会故意制造与部分像素大小列,但使用明星的大小(我确实使用了很多)时,可以很容易地观察到同样的效果。

此问题可以通过使用SnapsToDevicePixels属性(<Grid SnapsToDevicePixels="True">而不是<Grid>)解决。这是有效的,因为WPF具有内部一致的舍入,所以两列都捕捉到相同的像素。 但是,我遇到了一个非常相似的问题,我无法找到解决方案。

出于某种原因,使用几何级的时候,我得到了相同的排序像素对齐问题,我这段时间我没有找到任何形式的变通。就好像这些像素是圆形的,但现在几何图形被折断一个像素,留下一个1像素宽的孔。

示例代码:

<Window x:Class="Test.Window1" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="Window1" Height="300" Width="300"> 
<Grid SnapsToDevicePixels="False"> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="100.5" /> 
     <ColumnDefinition Width="199.5" /> 
    </Grid.ColumnDefinitions> 
    <Border Background="Red"> 
     <Grid.Column>1</Grid.Column> 
    </Border> 
    <Border> 
     <Border.Background> 
      <DrawingBrush> 
       <DrawingBrush.Drawing> 
        <GeometryDrawing Brush="Red"> 
         <GeometryDrawing.Geometry> 
          <RectangleGeometry Rect="0,0,1,1"></RectangleGeometry> 
         </GeometryDrawing.Geometry> 
        </GeometryDrawing> 
       </DrawingBrush.Drawing> 
      </DrawingBrush> 
     </Border.Background> 
    </Border> 
</Grid> 
</Window> 

不知道如何让我的像素正确对齐?

编辑:

正常工作,现在根据答案添加GuidelineSet之后。工作图代码是:

<DrawingGroup> 
    <DrawingGroup.GuidelineSet> 
     <GuidelineSet GuidelinesX="0,1" GuidelinesY="0,1"></GuidelineSet> 
    </DrawingGroup.GuidelineSet>     
    <GeometryDrawing Brush="Red"> 
     <GeometryDrawing.Geometry> 
      <RectangleGeometry Rect="0,0,1,1"></RectangleGeometry> 
     </GeometryDrawing.Geometry> 
    </GeometryDrawing> 
</DrawingGroup> 

回答

2

我相信你必须use a GuidelineSet in the case of a Drawing。关于如何在SDK中应用GuidelineSets here有一个很好的部分。

+0

谢谢!它工作得很好 - 用工作代码编辑我的问题。似乎奇怪的是,图纸不关心它们的容器的SnapsToDevicePixels,虽然,或至少DrawingGroup没有自己的财产SnapsToDevicePixels。我最终做了那个属性手动做的事情...... – 2009-10-20 08:59:12