0

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

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

    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 创建它。

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

<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" />

任何帮助,将不胜感激!

吉尔曼

编辑 :

这是用于 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; }
    }
}
4

2 回答 2

0

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

于 2012-06-19T14:09:37.900 回答
0

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

于 2012-06-19T14:34:46.573 回答