0

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

交叉.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(不使用 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);
        }
    }
}

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

4

1 回答 1

4

尝试使用 The ViewBoxControl ,将您的 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>
于 2012-11-19T22:36:01.223 回答