Note that there are some explanatory texts on larger screens.

plurals
  1. POwpf custom control get/set not firing
    primarykey
    data
    text
    <p>I'm learning WPF and, coming from Flex and AS, it seems overly complicated at times. Opinions aside, my problem is the following.</p> <p>I've created a custom control, ToolBarButton which is basically an image button that is destined to be included in a custom toolbar. I've added some properties to this control and I'd like to be able to set them from the XAML. Though the property appears in AutoCompletion on the XAML side, the Set method is never fired and the property stays null. So here's the ToolBarButton Code Behind : </p> <pre><code> 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); } } </code></pre> <p>You'll notice two constructors, one to create the control at runtime, the other to create it from XAML.</p> <p>And here's the XAML code that uses the custom control but doesn't fire the set method :</p> <pre><code>&lt;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" &gt; &lt;BaseControls:ToolBarButton Width="35" Height="35" ImgSource="Assets/i_zoomin.png" ConfigFileName="ZoomIn.xml" /&gt; </code></pre> <p></p> <p>Any help would be appreciated!</p> <p>Ggilmann</p> <p><strong>EDIT :</strong></p> <p>Here's the Base Class used for the ToolBarButton, it also has the same problem with it's properties.</p> <pre><code>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; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload