WPF用户控件调整大小

时间:2012-11-19 22:29:50

标签: c# wpf user-controls wpf-controls

我的目标是创建一个usercontrol,当我在设计器中更改其大小时将重绘。为简单起见,我创建了usercontrol Cross。 (我是WPF的初学者)。

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 (不使用语句):

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);
        }
    }
}

因此,当我将设计师中Cross的大小从300更改为600时,我希望有2行 - [0,0,600,600],[0,600,600,0]而不是[0,0,300,300],[0,300,300,0]。< / p>

1 个答案:

答案 0 :(得分:4)

尝试使用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>