• 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);
    }
    
  • 相关阅读:
    2017.3.17作业
    2017.3.16作业
    2017.3.15作业
    2017.3.14作业
    2017.3.13作业
    2017.3.10作业
    网站流量日志分析(数据 采集之 Flume 采集)
    网站流量日志分析(模块开发——数据仓库设计)
    日常问题及解决
    可爱的生活
  • 原文地址:https://www.cnblogs.com/wangjixianyun/p/3351522.html
Copyright © 2020-2023  润新知