简体   繁体   English

WPF用户控件调整大小

[英]WPF usercontrol resizing

My goal is to create a usercontrol which will redraw when I change its size in designer. 我的目标是创建一个用户控件,当我在设计器中更改其大小时会重绘。 For simplicity I created usercontrol Cross. 为简单起见,我创建了usercontrol Cross。 (I am rather a beginner in WPF). (我是WPF的初学者)。

Cross.xaml: Cross.xaml:

<UserControl x:Class="WpfApplication2.Cross"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid Name="Grid1">
    </Grid>
</UserControl>

Cross.xaml.cs (without using statements): Cross.xaml.cs (不使用using语句):

namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for Cross.xaml
    /// </summary>
    /// 
    public partial class Cross : UserControl
    {
        public Cross()
        {
            InitializeComponent();

            Line l = new Line();
            l.X1 = 0; l.Y1 = 0; l.X2 = 300; l.Y2 = 300;
            l.Stroke = new SolidColorBrush(Colors.Black);
            Grid1.Children.Add(l);
            l = new Line();
            l.X1 = 0; l.Y1 = 300; l.X2 = 300; l.Y2 = 0;
            l.Stroke = new SolidColorBrush(Colors.Black);
            Grid1.Children.Add(l);
        }
    }
}

So when I change the size of a Cross in designer from 300 to 600 I want there 2 lines - [0,0,600,600], [0,600,600,0] rather than [0,0,300,300], [0,300,300,0]. 因此,当我将设计器中Cross的大小从300更改为600时,我希望有2行-[0,0,600,600],[0,600,600,0],而不是[0,0,300,300],[0,300,300,0]。

Try using The ViewBox Control , Change your xaml to 尝试使用ViewBox控件,将xaml更改为

<UserControl x:Class="WpfApplication2.Cross"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Viewbox>
            <Grid Name="Grid1"/>
        </Viewbox>
    </Grid>
</UserControl>

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

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