0

I am making my first serious foray into Prism(Unity). I have a module with a toolbar control that gets loaded (properly) into the region that it is supposed to. This toolbar is a listbox with ItemsSource databound to the ToolButtons property on its ViewModel, the constructor for which instantiates and adds three ToolButtons to the ToolButtons collection.

My ToolButton class has three custom DependencyProperties: Title (string), ButtonFace (Image), ActiveDocumentCount (int). Styling is taken care of by a resource dictionary in the module with a Style and associated ControlTemplate. I have databound the properties, but none of the values or the image are displaying (other elements in the style are however) via TemplateBinding.

I am trying to debug the databinding, but to no avail. I do not get any massages pertinent in the Output window, and the 2nd and 3rd suggestions in this blog have produced no output either. I think that if I could get the verbose (i.e. PresentationTraceSources.TraceLevel=High) output, I could figure out what is happening on the databinding front.

EDIT:

Toolbutton Class

public class ToolButton : Button
{
    public ToolButton()
    {
        //DefaultStyleKeyProperty.OverrideMetadata(typeof(ToolButton), new FrameworkPropertyMetadata(typeof(ToolButton)));
    }

    public Image ButtonFace
    {
        get { return (Image)this.GetValue(ButtonFaceProperty); }
        set { this.SetValue(ButtonFaceProperty, value); }
    }
    public static readonly DependencyProperty ButtonFaceProperty =
        DependencyProperty.Register("ButtonFace", typeof(Image), typeof(ToolButton), new PropertyMetadata(null));

    public string Title
    {
        get { return (string)this.GetValue(TitleProperty); }
        set { this.SetValue(TitleProperty, value); }
    }

    public static readonly DependencyProperty TitleProperty =
        DependencyProperty.Register("Title", typeof(string), typeof(ToolButton), new PropertyMetadata(""));

    public int OpenRecordCount
    {
        get { return (int)this.GetValue(OpenRecordCountProperty); }
        set { this.SetValue(OpenRecordCountProperty, value); }
    }

    public static readonly DependencyProperty OpenRecordCountProperty =
        DependencyProperty.Register("OpenRecordCount", typeof(int), typeof(ToolButton), new PropertyMetadata(null));

}
CodeWarrior
  • 7,388
  • 7
  • 51
  • 78
  • 1
    Can you show the bit of code you are using for Registering your DependencyProperties. Also are you doing any SetValues on them anywhere? as that would destroy the Bindings. – Colin Smith Jul 31 '12 at 19:06
  • Added the ToolButton class to my post. I am using SetValue throughout. I was unaware that SetValue would destroy bindings. How else should I run the setter? – CodeWarrior Aug 01 '12 at 02:56
  • 1
    Those DPs look ok SetValue in the CLR backed property is fine....but if you are anyone is setting a local value by calling them (via code) then that will destroy the binding. http://wpf.2000things.com/2010/12/06/147-use-setcurrentvalue-when-you-want-to-set-a-dependency-property-value-from-within-a-control/ – Colin Smith Aug 01 '12 at 09:03
  • Ahh, I see. I see what is going on, and thank you. Hook me up with an answer saying as much and I will send some rep your way. – CodeWarrior Aug 01 '12 at 14:09

1 Answers1

1

Those DPs look ok SetValue in the CLR backed property is fine....but if you or anyone is setting a local value on those properites (e.g. by calling your CLR backed properties or DependencyObject.SetValue) then that will destroy the binding.

Related links:

Community
  • 1
  • 1
Colin Smith
  • 12,375
  • 4
  • 39
  • 47