0

我有一个正在测试的 WPF 应用程序,它加载 XML 并将其可视化在用户控件中。现在我遇到的问题是每次加载用户控件时HorizontalAlignment都可以,但是VerticalAlignment不适合用户控件的高度大小。

任何人都知道如何解决这个问题?

主窗口.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>

主窗口.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 页面是标准的 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);
            }
        }
    }

结果

应用结果

4

2 回答 2

0

不要将您的 Control 添加到 aGrid中,而是使用ContentControl并将其添加到其 Content-Property。它还会自动拉伸它的孩子。

于 2012-10-18T08:50:59.197 回答
0

我将 MainWindowSizeToContent放到WidthAndHeight.

于 2012-10-18T09:20:33.490 回答