简体   繁体   English

WPF用户控件超出网格范围

[英]WPF usercontrol out of bounds of the Grid

I have a WPF app that I'm testing which loads a XML and visualizes it in a usercontrol. 我有一个正在测试的WPF应用程序,该应用程序加载XML并将其可视化在用户控件中。 Now the issue I'm having is that every time I load my user control the HorizontalAlignment is okay, but the VerticalAlignment doesn't adept to the height size of the usercontrol. 现在我遇到的问题是,每次加载用户控件时, HorizontalAlignment都可以,但是VerticalAlignment并不能适应用户控件的高度大小。

Anyone has an idea how to solve this? 有人知道如何解决这个问题吗?

MainWindow.xaml MainWindow.xaml

        <Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:UserControls="clr-namespace:RackBuildingTesT.UserControls" x:Class="RackBuildingTesT.MainWindow"
    Title="MainWindow" Height="578" Width="758" SizeChanged="Window_SizeChanged_1">
<Grid>
        <DockPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <Border DockPanel.Dock="Top" BorderBrush="Black" BorderThickness="1">
                <Grid>
                    <Label Content="Welke rack laden" HorizontalAlignment="Left" VerticalAlignment="Top"/>
                    <ComboBox x:Name="RackChooser" HorizontalAlignment="Left" Margin="100,0,0,0" VerticalAlignment="Top" Width="120" SelectionChanged="RackChooser_SelectionChanged"/>
                </Grid>
            </Border>
        <Border DockPanel.Dock="Top" BorderBrush="Black" BorderThickness="1">
            <Grid x:Name="RackGrid" Margin="0,0,0,0">
            </Grid>
        </Border>
        </DockPanel>
    </Grid>

MainWindow.xaml.cs MainWindow.xaml.cs

        public MainWindow()
    {
        InitializeComponent();
        LoadRackCombo();
    }
    private void LoadRackCombo()
    {
        var files = Directory.GetFiles(Properties.Settings.Default.FilePathXMLRack);
        foreach (var item in files)
        {
            RackChooser.Items.Add(item);
        }
    }

    private void RackChooser_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        this.RackGrid.Children.Clear();
        this.Cursor = Cursors.Wait;
        if (RackChooser.SelectedIndex == -1)
            MessageBox.Show("Select a rack");
        else
        {
            string rackFile = Convert.ToString(RackChooser.Items[RackChooser.SelectedIndex]);
            UserControls.RackBuilder r = new UserControls.RackBuilder();
            RackGrid.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
            RackGrid.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
            r.Width = RackGrid.Width;
            r.Height = RackGrid.Height;
            var _r = TRack.CreateFromXMLFile(rackFile, null);
            r.set_Rack(_r);
            RackGrid.Children.Add(r);
        }
        this.Cursor = Cursors.Arrow;
    }

    private void Window_SizeChanged_1(object sender, SizeChangedEventArgs e)
    {
        RackGrid.Width = this.Width;
        RackGrid.Height = this.Height;
    }

RackBuilder.xaml.cs (xaml page is standard WPF usercontrol) RackBuilder.xaml.cs(xaml页面是标准的WPF用户控件)

        public RackBuilder()
    {
        InitializeComponent();
    }

    public TRack fRack { get; set; }

    public void set_Rack(TRack value)
    {
        this.fRack = value;
        this.InvalidateVisual();
    }

    protected override void OnRender(DrawingContext drawingContext)
    {
        if (this.fRack != null)
        {
            var xScale = this.Width / this.fRack.Size.Width;
            var yScale = this.Height / this.fRack.Size.Height;
            var smallest = 0.0;

            if (xScale < yScale)
                smallest = xScale;
            else
                smallest = yScale;

            foreach (var hole in this.fRack.HolePositions)
            {
                drawingContext.DrawEllipse(Brushes.Aquamarine, null, new Point(hole.Position.X * xScale, hole.Position.Y * yScale),
                                           hole.Diameter * smallest * 0.5, hole.Diameter * smallest * 0.5);
            }
        }
    }

Result 结果

应用结果

Instead of adding your Control to a Grid , use the ContentControl and add it to its Content-Property. 不用将控件添加到Grid ,而是使用ContentControl并将其添加到其Content-Property中。 It also stretches its child automatically. 它还会自动拉伸孩子。

我通过将MainWindow SizeToContentWidthAndHeight修复了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM