在WPF中查看错误的应用程序

时间:2019-08-21 09:01:09

标签: c# wpf

我有一个应用程序,当屏幕尺寸改变时,它会破坏图像

下面是屏幕

错误在哪里?什么会导致这种情况?我什至不知道在哪里看,因为它不是一次又一次消失的永久错误

这是我在每个视图中使用行和数字加载项目的方式:

<ItemsControl ItemsSource="{Binding ConnectorsGridsHorizontal, Mode=TwoWay}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Grid />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

功能图1个元素:

public Grid DrawConnector1(Thickness margin, int nr, bool rotate, bool rightSide)
{
    Grid grid = new Grid();
    grid.Margin = margin;

    grid.HorizontalAlignment = HorizontalAlignment.Left;
    grid.Width = S10;
    grid.Height = 128;
    grid.VerticalAlignment = System.Windows.VerticalAlignment.Center;

    Line line = new Line();
    line.X1 = S10HALF;
    line.X2 = S10HALF;
    line.Y1 = 0;
    line.Y2 = 128;
    line.StrokeDashArray = new System.Windows.Media.DoubleCollection() { 4, 2, 1, 2 };

    line.Stroke = System.Windows.Media.Brushes.Green;
    line.StrokeThickness = 4;

    grid.Children.Add(line);

    Grid inGrid = new Grid();
    inGrid.Width = S10;
    inGrid.Height = HF;
    inGrid.Background = System.Windows.Media.Brushes.Black;

    grid.Children.Add(inGrid);

    Border br = new Border();
    br.BorderThickness = new Thickness(0);
    br.CornerRadius = new CornerRadius(10);
    br.Background = System.Windows.Media.Brushes.White;
    br.Margin = new Thickness(-10, -10, -10, -10);
    br.Width = 20;
    br.Height = 20;
    br.VerticalAlignment = rightSide ? VerticalAlignment.Top : VerticalAlignment.Bottom;
    br.RenderSize = new System.Windows.Size(br.ActualWidth + 1, br.ActualHeight + 1);

    if (SelectedConnector + 1 == nr) br.Background = System.Windows.Media.Brushes.Green;
    else br.Background = System.Windows.Media.Brushes.White;

    if (DisableGreenMark) br.Background = System.Windows.Media.Brushes.White;

    TextBlock txtBlock = new TextBlock();
    txtBlock.FontSize = 16;
    txtBlock.Text = nr.ToString();
    txtBlock.Foreground = new SolidColorBrush(Colors.Black);
    txtBlock.VerticalAlignment = VerticalAlignment.Center;
    txtBlock.HorizontalAlignment = HorizontalAlignment.Center;

    br.Child = txtBlock;

    grid.Children.Add(br);

    Binding b = new Binding("S60_20");
    b.Mode = BindingMode.Default;
    grid.SetBinding(Canvas.TopProperty, b);

    return grid;
}

GIF:

enter image description here

我也在使用 SnapsToDevicePixels="true"

1 个答案:

答案 0 :(得分:2)

首先,尝试执行以下操作:

br.HorizontalAlignment = HorizontalAlignment.Stretch;
br.Margin = new Thickness(0, 0, 0, 0);
  • 请勿使用负值的边距
  • Stretch将为您提供元素扩展

侧切可能是由于元素的条带太窄造成的:

您有:

grid.Width = S10;

line.X1 = S10HALF;
line.X2 = S10HALF;

这表明您将宽度设置为10

在这里,您已设置为20

br.Width = 20;
br.Height = 20;

我还建议在此处进行更改:

grid.Width = S10 * 2;

line.X1 = S10HALF * 2;
line.X2 = S10HALF * 2;