wpf自定义控件get / set not firing

时间:2012-06-19 13:57:15

标签: c# wpf xaml custom-controls

我正在学习WPF,来自Flex和AS,有时似乎过于复杂。除了意见之外,我的问题如下。

我已经创建了一个自定义控件ToolBarButton,它基本上是一个注定要包含在自定义工具栏中的图像按钮。我已经为这个控件添加了一些属性,我希望能够从XAML中设置它们。虽然该属性出现在XAML端的AutoCompletion中,但从不触发Set方法,并且该属性保持为null。所以这里是ToolBarButton Code Behind:

    public static readonly DependencyProperty ImgSrcProperty = DependencyProperty.RegisterAttached("ImgSource", typeof(string), typeof(ToolBarButton));

    public static readonly DependencyProperty OnClickProperty = DependencyProperty.Register("OnClick", typeof(RoutedEventHandler), typeof(ToolBarButton));

    public ToolBarButton(RoutedEventHandler OnClick, string imgSrc, Map map = null, string ConfigFile = null) :
        base(ConfigFile, map)
    {
        if (OnClick != null) SetValue(OnClickProperty, OnClick);

        if (imgSrc != null) SetValue(ImgSrcProperty, imgSrc);

        this.AddChild(CreateButton());

        InitializeComponent();
    }

    public ToolBarButton() : this(null, null) { }

    private Button CreateButton()
    {
        BitmapImage icon = new BitmapImage();
        icon.BeginInit();
        icon.UriSource = new Uri(ImgSource, UriKind.Relative);
        icon.EndInit();

        Image img = new Image();
        img.Stretch = Stretch.Fill;
        img.Source = icon;

        Button BtnToAdd = new Button();
        BtnToAdd.Width = 35;
        BtnToAdd.Height = 35;
        BtnToAdd.Content = img;
        BtnToAdd.Background = new ImageBrush(icon);

        BtnToAdd.Click += OnClick;

        return BtnToAdd;
    }


    public string ImgSource
    {
        get { return (string)GetValue(ImgSrcProperty); }
        set { SetValue(ImgSrcProperty, value); }
    }

    public RoutedEventHandler OnClick
    {
        get { return (RoutedEventHandler)GetValue(OnClickProperty); }
        set { SetValue(OnClickProperty, value); }
    }

你会注意到两个构造函数,一个用于在运行时创建控件,另一个用于从XAML创建控件。

这是使用自定义控件的XAML代码,但不会触发set方法:

<BaseControls:ToolBar 
         x:Class="Basic_Mapping.Widgets.NavigationToolBar"
         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" 
         xmlns:BaseControls="clr-namespace:Basic_Mapping.Base_Controls"
         mc:Ignorable="d" >

<BaseControls:ToolBarButton Width="35" Height="35" ImgSource="Assets/i_zoomin.png"  ConfigFileName="ZoomIn.xml" />

任何帮助将不胜感激!

Ggilmann

编辑:

这是用于ToolBarButton的基类,它的属性也有同样的问题。

public partial class ConfigurableUserControl : UserControl
{
    private XmlDocument configXML;

    public static readonly DependencyProperty XmlProperty = DependencyProperty.Register("ConfigFileName", typeof(string), typeof(ConfigurableUserControl));

    public static readonly DependencyProperty MapProperty = DependencyProperty.Register("Map", typeof(Map), typeof(ConfigurableUserControl));

    public ConfigurableUserControl(string configFile, Map map)
    {
        if (configFile != null) SetValue(XmlProperty, configFile);

        if (map != null) SetValue(MapProperty, map);

        string file = (string)GetValue(XmlProperty);

        if (file != null)
        {
            configXML = new XmlDocument();

            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..\\..\\Config\\" + configFile);

            if (File.Exists(path)) configXML.Load(path);
        }
    }

    public ConfigurableUserControl() : this(null, null) { }

    public string ConfigFileName
    {
        //get { return (string)GetValue(XmlProperty); }
        set { SetValue(XmlProperty, value); }
    }

    public Map Map
    {
        get { return (Map)GetValue(MapProperty); }
        set { SetValue(MapProperty, value); }
    }

    public XmlDocument ConfigXML
    {
        get { return configXML; }
    }
}

2 个答案:

答案 0 :(得分:0)

尝试将InitializeComponent();放在构造函数的开头而不是当前的末尾。

答案 1 :(得分:0)

我的猜测是这个问题,以及你对基类的问题是由于你没有实现INotifyPropertyChanged并进行适当的调用。