WP7 - 通过绑定更新UserControl

时间:2011-08-15 23:52:25

标签: silverlight windows-phone-7 dependency-properties

我有一个WP7 silverlight项目。我有一个'UserControl'来显示基于DependencyProject的矩形。

现在,当我使用以下内容将控件放在我的页面上时:

<uc:RectangleControl NumRectangles={Binding Path=RectangleCount,Mode=OneWay} />

我根据'RectangleCount'的初始值显示我的矩形。但是,更改RectangleCount时,用户控件永远不会更新。我希望这是由于我在OnLoaded事件中绘制矩形引起的,但我不能在我的生活中找到另一个要绑定的事件,以使控件在NumRectangles DP更新时绘制新的矩形。

任何帮助?

UserControl XAML:

<UserControl x:Class="WP7Test.Controls.RectangleControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    d:DesignHeight="80" d:DesignWidth="480">

    <Canvas x:Name="canvas" 
            Height="{Binding Height}" Width="{Binding Width}">

    </Canvas>
</UserControl>

在后面的代码中,我根据依赖属性添加了矩形:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace WP7Test.Controls
{

    public partial class RectangleControl : UserControl
    {

        private double RectangleSpacing = 1;


        #region Dependency Properties




        public int NumRectangles
        {
            get { return (int)GetValue(NumRectanglesProperty); }
            set { SetValue(NumRectanglesProperty, value); }
        }

        // Using a DependencyProperty as the backing store for NumRectangles.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty NumRectanglesProperty =
            DependencyProperty.Register("NumRectangles", typeof(int), typeof(RectangleControl), new PropertyMetadata(5));


        #endregion

        public RectangleControl()
        {
            InitializeComponent();

            Loaded += new RoutedEventHandler(OnLoaded);
        }

        private void OnLoaded(object sender, RoutedEventArgs args)
        {

        double rectangleWidth = 20;
double rectangleHeight = 20;
double x = 0;
double y = 0;

            for (int i = 0; i < NumRectangles; i++)
            {
                Brush b = new SolidColorBrush(Colors.Red);


                Rectangle r = GenerateRectangle(x, y, rectangleWidth, rectangleHeight, b);
                canvas.Children.Add(r);

                x += rectangleWidth + 1;
            }


        }

        public Rectangle GenerateRectangle(double x, double y, double width, double height, Brush brush)
        {
            Rectangle r = new Rectangle();
            r.Height = height;
            r.Width = width;
            r.Fill = brush;
            Canvas.SetLeft(r, x);
            Canvas.SetTop(r, y);
            return r;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

注册依赖项属性时,需要为PropertyMetadata中的“已更改”事件提供处理程序。请参阅此处的MSDN文档:

http://msdn.microsoft.com/en-us/library/ms557330%28VS.95%29.aspx

作为依赖项属性更改处理程序提供的方法将是静态的,因此您需要使用传递给此方法的参数来获取对控件的引用。从这里,您可以清除并重新构建您的UI。