zoukankan      html  css  js  c++  java
  • 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);
    }
    
  • 相关阅读:
    【Codevs 2630】宝库通道
    【Codevs 2115】数集分割
    【HDU2037】今年暑假不AC
    【Codeforces】Round #376 (Div. 2)
    【Dairy】2016.10.17-1 OIer最悲剧的事情
    【Codevs 3115】高精度练习之减法
    【Codevs1080】质数环
    【T^T 1871】获取敌情
    【Codevs3151】交通管制I
    【Codeforces】716D Complete The Graph
  • 原文地址:https://www.cnblogs.com/wangjixianyun/p/3351522.html
Copyright © 2011-2022 走看看