我正在学习WPF,而且来自Flex和AS,有时似乎过于复杂 . 除了意见之外,我的问题如下 .
我已经创建了一个自定义控件ToolBarButton,它基本上是一个图像按钮,注定要包含在自定义工具栏中 . 我已经为这个控件添加了一些属性,我希望能够从XAML中设置它们 . 虽然该属性出现在XAML端的AutoCompletion中,但从不触发Set方法,并且该属性保持为null . 所以这里是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" />
任何帮助,将不胜感激!
Ggilmann
EDIT :
这是用于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 回答
我的猜测是这个问题,以及你对基类的问题是由于你没有实现INotifyPropertyChanged并进行适当的调用 .
尝试将
InitializeComponent();
放在构造函数的开头而不是当前的末尾 .