Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF自定义控件属性-分配对象,如页面_C#_Wpf - Fatal编程技术网

C# WPF自定义控件属性-分配对象,如页面

C# WPF自定义控件属性-分配对象,如页面,c#,wpf,C#,Wpf,是否可以在自定义控件中存储对页面的引用,以便在单击该控件时加载页面(如自定义菜单项) 到目前为止,我的代码是: public class ccMenuItem : Button { public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(Control)); public static readon

是否可以在自定义控件中存储对页面的引用,以便在单击该控件时加载页面(如自定义菜单项)

到目前为止,我的代码是:

public class ccMenuItem : Button
{
     public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(Control));
     public static readonly DependencyProperty BackColorProperty = DependencyProperty.Register("BackColor", typeof(SolidColorBrush), typeof(Control), new UIPropertyMetadata(Brushes.White));
     public static readonly DependencyProperty PageProperty = DependencyProperty.Register("Page", typeof(Page), typeof(Control));


     public Page Page
     {
         get { return (Page)GetValue(PageProperty); }
         set { SetValue(PageProperty, value); }
     }

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

    static ccMenuItem()
    {

        DefaultStyleKeyProperty.OverrideMetadata(typeof(ccMenuItem), new FrameworkPropertyMetadata(typeof(ccMenuItem)));
    }
}
代码编译正常,但是如何在XAML中将页面(比如称为vpn的类)分配到页面属性?

如果您的“应用程序”使用的是
导航窗口而不是
窗口
,那么您可以访问
导航服务
,并告诉它更改页面

protected override void OnClick()
{
    NavigationService ns = NavigationService.GetNavigationService(this);

    ns.Navigate( Page );
}