页面中的依赖属性

时间:2011-09-03 10:43:55

标签: c# .net wpf xaml

我想在我的Window类中定义一个DependencyProeprty,如下面的代码。

namespace dgCommon
{

    //MenuPage is a Page.
    public partial class MenuPage : IFrameInterop
    {

        public Style MenuIconStyle { get { return (Style)GetValue(MenuIconStyleProperty); } set { SetValue(MenuIconStyleProperty, value); } }
        public static readonly DependencyProperty MenuIconStyleProperty = DependencyProperty.Register("MenuIconStyle", typeof(Style), typeof(MenuPage), new UIPropertyMetadata(null));
    ...

在自定义控件中,此代码启用依赖项属性。但是在一个页面中,遵循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" 

      <!--following line is problem.-->
      MenuIconStyle="{StaticResource MenuButtonStyle}"

      x:Name="pageMenu">
...

是什么原因?

3 个答案:

答案 0 :(得分:2)

你不能使用在声明它的控件/窗口/页面的XAML中分配依赖属性。如果要设置其默认值,请在代码隐藏中执行此操作。

答案 1 :(得分:1)

这个问题已在这里得到解答:Setting a custom property within a WPF/Silverlight page
这个问题的原因也在链接中解释。

您可以选择在Xaml中分配自定义依赖项属性

选项1 。为您添加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); }
    }
}

选项2。为MenuIconStyle实现静态get和set方法

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);
    }
    // ...
}

选项3。使用其他人指出的附加属性。

答案 2 :(得分:0)

正如托马斯所说,要在窗口的代码隐藏中为DP提供默认值,请替换

带有new UIPropertyMetadata(null)

new UIPropertyMetadata(DEFAULT_VALUE_HERE)

也就是说,如果你在视图中没有引用它,DP就没用了,你可以先通过给你的控件命名来访问xaml中的这个DP:

<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" 
      x:Name="pageMenu" />

然后像这样呼叫DP:

<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" 
      x:Name="pageMenu" Style="{Binding MenuIconStyle, ElementName=pageMenu}" />

现在,如果您真的想要一个名为MenuIconStyle的Window上的Property,您将需要查看attached properties