Question

I have created a Custom UserControl that exposes some DependencyProperties like HeaderTitle and HeaderTitleForeground

public partial class PageHeaderControl : UserControl
{
    public string HeaderTitle 
    {
        get { return (string)GetValue(HeaderTitleProperty); }
        set { SetValue(HeaderTitleProperty, value); }
    }

    public static readonly DependencyProperty HeaderTitleProperty = DependencyProperty.Register("HeaderTitle", typeof(string), typeof(PageHeaderControl), new PropertyMetadata(""));

    public string HeaderTitleForeground
    {
        get { return (string)GetValue(HeaderTitleForegroundProperty); }
        set { SetValue(HeaderTitleForegroundProperty, value); }
    }

    public static readonly DependencyProperty HeaderTitleForegroundProperty = DependencyProperty.Register("HeaderTitleForeground", typeof(string), typeof(PageHeaderControl), new PropertyMetadata(""));

    public PageHeaderControl()
    {
        InitializeComponent();
        (this.Content as FrameworkElement).DataContext = this;
    }
}

But while I am debugging my app it throws an Exception as I show below:

  System.Exception occurred
   _HResult=-2146233088
   _message=Error HRESULT E_FAIL has been returned from a call to a COM component.
   HResult=-2146233088
   Message=Error HRESULT E_FAIL has been returned from a call to a COM component.
   Source=System.Windows
   StackTrace:
      at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
   InnerException: 

However the Custom Control is correctly drawn. So, how can I resolve this? Is this a critical issue?

Was it helpful?

Solution 2

I've found my mistake. It was the type of HeaderTitleForeground, so I just changed from string to SolidColorBrush and added SolidColorBrush(Colors.Black) to PropertyMetadata. Here's the fixed version for the UserControl:

public partial class PageHeaderControl : UserControl
{
public string HeaderTitle 
{
    get { return (string)GetValue(HeaderTitleProperty); }
    set { SetValue(HeaderTitleProperty, value); }
}

public static readonly DependencyProperty HeaderTitleProperty = DependencyProperty.Register("HeaderTitle", typeof(string), typeof(PageHeaderControl), new PropertyMetadata(""));

public SolidColorBrush HeaderTitleForeground
{
    get { return (SolidColorBrush)GetValue(HeaderTitleForegroundProperty); }
    set { SetValue(HeaderTitleForegroundProperty, value); }
}

public static readonly DependencyProperty HeaderTitleForegroundProperty = DependencyProperty.Register("HeaderTitleForeground", typeof(SolidColorBrush), typeof(PageHeaderControl), new PropertyMetadata(new SolidColorBrush(Colors.Black)));

public PageHeaderControl()
{
    InitializeComponent();
    (this.Content as FrameworkElement).DataContext = this;
}
}

OTHER TIPS

Your class is PageHeaderControl but your dependency props are registered with ElevationPageHeaderControl as their owner..?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top