首页 新闻 会员 周边

WPF 容器缩放的一个问题

0
悬赏园豆:15 [已关闭问题] 关闭于 2013-05-07 22:54

  我自定义了一个容器控件,当某个子元素过高的时候会按比例缩放所有元素,以保证所有元素高度不会超过这个容器.

我的实现是这样的

1.容器跟子元素都有一个Scale属性表示当前的缩放比例.

2.子元素的Scale要Bind到容器的Scale.

3.当某个子元素超过容器高度时,容器算一个新的Scale出来.

4.在子元素的Scale属性包装器中重算一下高度.

xaml代码:

 

    <Style TargetType="{x:Type local:ZoomPanel}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:ZoomPanel}">
                    <ItemsPresenter/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        
        <Setter Property="ItemTemplate">
            <Setter.Value>
                <loca:ZoomControl Scale="{Binding RelativeSource={RelativeSource AncestorType=local:ZoomPanel}, Path=Scale}"/>
            </Setter.Value>
        </Setter>
    </Style>

自定义的容器控件一些代码

private double _scale = 1;
public double Scale
{
     get
     {
         return _scale;
     }
     set
     {
         _scale = value;
         NotifyPropertyChanged("Scale");       //通知Bind去改变子元素的Scale属性
     }
}

子元素的代码

//子元素的Scale注册为依赖属性是因为要Bind到容器控件
public double Scale
{
       get { return (double)GetValue(ScaleProperty); }
       set
       {
              this.Height *= value;      //问题出现在这里,容器的Scale通过Bind改变了子元素的Scale值时,这里居然没执行到,这样我就不能改变高度了.
              SetValue(ScaleProperty, value);
       }
}
        public static readonly DependencyProperty ScaleProperty = DependencyProperty.Register("Scale", typeof(double), typeof(ZoomControl), new UIPropertyMetadata(1.0));

我也有想过子元素的Scale用普通属性,容器Scale改变是Foreach一下,但是通过Bind能解决那就更好了,为什么会出现这种情况呢?

 

灰机_不会飞的主页 灰机_不会飞 | 初学一级 | 园豆:8
提问于:2013-05-07 18:25
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册