WP7 - 通过绑定更新 UserControl

发布于 2024-11-29 17:40:32 字数 3200 浏览 0 评论 0原文

我有一个 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;
        }
    }
}

I have a WP7 silverlight project. I have a 'UserControl' to display rectangles based on a DependencyProject.

Now, when I place the control on my page with the following:

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

I get my rectangles displayed based on initial value of 'RectangleCount'. However, when RectangleCount is changed, the User control never updates. I expect this is caused by me drawing my rectangles in the OnLoaded event, but I can't for the life of me find another event to bind to to cause the control to draw new rectangles when the NumRectangles DP is updated.

Any help?

The 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>

And in the code behind, I add rectangles based on a dependency property:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稀香 2024-12-06 17:40:32

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

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

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

When you register your dependency property, you need to provide a handler for the 'changed' event within your PropertyMetadata. See the MSDN documentation here:

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

The method that you supply as a dependency property change handler will be static so you need to use the arguments passed to this method to obtain a reference to your control. From here you can clear and re-build your UI.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文