wpf 调整主窗口大小时调整画布大小

qf9go6mv  于 2022-12-14  发布在  其他
关注(0)|答案(1)|浏览(286)

我正在尝试调整TabControl中包含的画布的大小,我希望在执行时调整窗口大小时也调整画布的大小
我尝试使用事件SizeChanged重新恢复画布,然后缩放它,但它不起作用,它不调整画布内的元素,即矩形,它们只是在我缩小窗口时消失
这是我正在使用的XAML代码

<Window x:Class="practica_final.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:customer_purchases"
        mc:Ignorable="d"
        Title="Customer" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition/>
            <RowDefinition Height="auto"/>
        </Grid.RowDefinitions>
        <StackPanel Grid.Column="0" Grid.Row="0">
            <Button Name="btnCustomer" Content="Add Customer" Margin="10" Click="btnCustomer_Click"/>
        </StackPanel>
        <StackPanel Grid.Row="2">
            <Button Name="btnExit" Content="Exit" Margin="10" Background="Tan" Click="btnExit_Click"/>
            <Button Name="btnHelp" Content="Help" Margin="10" Click="btnHelp_Click"/>
        </StackPanel>
        <Border BorderBrush="Black" BorderThickness="1"  Grid.RowSpan="2" Grid.Column="1" Grid.Row="1" />
        <TabControl Grid.RowSpan="2" Grid.Column="1" Grid.Row="1">
            <TabItem Name="tabItemCustomer" Header="Customer">
                <Canvas Name="canvasCustomer" Background="LightYellow"
                            SizeChanged="canvasCustomer_SizeChanged"/>
            </TabItem>
            <TabItem Name="tabItemPurchases" Header="Purchases">
                <Canvas Name="canvasPurchases" Background="CornflowerBlue"
                            SizeChanged="canvasPurchases_SizeChanged"/>
            </TabItem>
        </TabControl>
    </Grid>
</Window>

在MainWindow.xaml.cs中,我有sizeChanged代码

namespace customer_purchases{  
    public partical class MainWindow : Window {      
        private ScaleTransform sc;
        private TranslateTransform tt;
        private TransformGroup tg;

        public MainWindow() 
        {
            InitializeComponent();
            sc = new ScaleTransform();
            tt = new TranslateTransform();
            tg = new TransformGroup();
            tg.Children.Add(tt);
            tg.Children.Add(sc);
        }

        [...]

        private void canvasCustomer_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            width = canvasCoche.ActualWidth;
            height = canvasCoche.ActualHeight;

            tt.X = 10;
            tt.Y = 10;

            sc.ScaleX = ancho / 20;
            sc.ScaleY = alto / 20;
        }
    }
}

我也尝试过将Canvas包含到ViewBox中,但在执行时无法正确查看结果

unguejic

unguejic1#

你不需要听任何大小变化的事件,只需在画布周围使用一个视图框,如下所示:

<Viewbox>
    <Canvas Height="100" Width="100" Background="Lime">
        <Rectangle Canvas.Top="10" Canvas.Left="20" Height="10" Width="10" Fill="hotpink"/>
    </Canvas>
</Viewbox>

画布会将其内容拉伸到父级的大小,但是,硬编码画布大小以使其按预期工作是很重要的。

相关问题