Advertisement
Advertisement

新足迹

 找回密码
 注册
新足迹 门户 IT专业论坛 查看内容

问一个wpf问题

2011-5-5 21:16| 发布者: linkspeed | 查看: 1191| 原文链接

因为今天偶然遇到一个奇怪的问题:

建立一个wpf application
加一个custom control,并且给他加个背景颜色red
<UserControl x:Class="WpfApplication2.UserControl1"
             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"
             Background="Red"
             designHeight="300" designWidth="300">

</UserControl>

然后在custom control里加这么一个函数
   protected override Size ArrangeOverride(Size arrangeBounds)
        {
            this.Width = arrangeBounds.Width;
            this.Height = arrangeBounds.Height;
            return base.ArrangeOverride(arrangeBounds);
        }

在main window里面直接调用user control.
<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:t="clr-namespace:WpfApplication2"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <t:UserControl1></t:UserControl1>
    </Grid>
</Window>
其他代码都是vs.net 自动的。

按照想当然的逻辑,ArrangeOverride传入的是大小,然后user control的大小也应该变化。
而实际上不是,实际上这2行加了,反而无法随窗口变化大小。不加反而可以。
如果把arrangeBounds的width和height输出到一个文本文件里面,
可以发现,如果这2行不加,arrangeBounds的大小是随着窗口大小变化的,
如果加了,反而变成固定的值了。
Advertisement
Advertisement


Advertisement
Advertisement
返回顶部