WPF自定义组件,自定义属性(依赖属性)

正常定义的以来属性,在XAML里设置值得时候是不触发Setter的,只能在code中用。
监听PropertyChangedCallback事件可以感知XAML里的设置,这样才能code和XAML配合使用。
http://stackoverflow.com/questions/5644081/wpf-usercontrol-dependency-property-setter-not-triggering
<UserControl x:Class="WpfApplication1.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" 
             d:DesignHeight="300" d:DesignWidth="300" Background="Black">
    <Grid>
        <Button Name="b" Content="Button" Margin="27,26,0,0" Click="b_Click" Height="22" VerticalAlignment="Top" HorizontalAlignment="Left" Width="75"/>

    </Grid>
</UserControl>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// UserControl1.xaml の相互作用ロジック
    /// </summary>
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        public static readonly DependencyProperty IsAAAProperty = 
            DependencyProperty.Register(
            "IsAAA", typeof(Boolean),
             typeof(UserControl1),
              new FrameworkPropertyMetadata(new PropertyChangedCallback(OnUriChanged)
                  
              )
            );

        private static void OnUriChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue == true)
            {
                UserControl1 u = (UserControl1)d;
                u.b.Background = Brushes.Black;
            }
            MessageBox.Show("event");
        }

        public bool IsAAA
        {
            get {
                MessageBox.Show("get");
                return (bool)GetValue(IsAAAProperty);
            }
            set {
                MessageBox.Show("set");
                SetValue(IsAAAProperty, value);
            }
        }

        private void b_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}

 http://www.cjavaphp.com/q/answers-dependency-property-not-working-trying-to-set-through-style-setter-1593523.html

The correct way of implementing a source for an Image in a user control in my opinion is not BitmapSouce. The easiest and best way (according to me again) is using Uri.

Change your dependency property to this (while also defining a change callback event):

ImageSourceProperty = DependencyProperty.Register(
    "ImageSource", typeof (Uri), typeof (MyButton),
    new FrameworkPropertyMetadata(new PropertyChangedCallback(OnImageSourceChanged)));

and the property to this:

public Uri ImageSource
{
    get
    {
           return (Uri)GetValue(ImageSourceProperty);
    }
    set
    {
           SetValue(ImageSourceProperty, value);
    }
}

Where your call back is like this:

private static void OnImageSourceChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
    MyButton hsb = (MyButton)sender;

    Image image = hsb.tehImage;
    image.Source = new BitmapImage((Uri) e.NewValue);
}
posted @ 2013-10-04 15:11  fff8965  阅读(9176)  评论(0编辑  收藏  举报