This question has been answered here: Setting a custom property within a WPF/Silverlight page
The reason for this problem is also explained in the link.
You have a few options to assign your custom Dependency Property in Xaml
Option 1. Create a base class for Page where you add your DP
MenuPage.xaml
<dgCommon:MenuPageBase x:Class="dgCommon.MenuPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dgCommon="clr-namespace:dgCommon"
MenuIconStyle="{StaticResource MenuButtonStyle}">
<!--...-->
</dgCommon:MenuPageBase>
MenuPage.xaml.cs
public partial class MenuPage : MenuPageBase
{
// ...
}
MenuPageBase.cs
public class MenuPageBase : Page
{
public static readonly DependencyProperty MenuIconStyleProperty =
DependencyProperty.Register("MenuIconStyle",
typeof(Style),
typeof(MenuPage),
new UIPropertyMetadata(null));
public Style MenuIconStyle
{
get { return (Style)GetValue(MenuIconStyleProperty); }
set { SetValue(MenuIconStyleProperty, value); }
}
}
Option 2. Implement static get and set methods for MenuIconStyle
MenuPage.xaml
<Page x:Class="dgCommon.MenuPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dgCommon="clr-namespace:dgCommon"
dgCommon.MenuPage.MenuIconStyle="{StaticResource MenuButtonStyle}">
MenuPage.xaml.cs
public partial class MenuPage : Page
{
public static readonly DependencyProperty MenuIconStyleProperty =
DependencyProperty.Register("MenuIconStyle",
typeof(Style),
typeof(MenuPage),
new UIPropertyMetadata(null));
public Style MenuIconStyle
{
get { return (Style)GetValue(MenuIconStyleProperty); }
set { SetValue(MenuIconStyleProperty, value); }
}
public static void SetMenuIconStyle(Page element, Style value)
{
element.SetValue(MenuIconStyleProperty, value);
}
public static Style GetMenuIconStyle(Page element)
{
return (Style)element.GetValue(MenuIconStyleProperty);
}
// ...
}
Option 3. Use Attached Properties as other people have pointed out.